I tried to use javascript and jquery to set the href attribute of tag since I need to download pictures from canvas.However, I confronted some problems. I couldn't figure out the reason, so I post my question here.
If I use id to set the listener, the listener function doesn't catch the event, just like the png button beneath.
Compare the jpegBtn2 and jpegBtn, I used plain javacsript to set the former href attribute and for the latter one, I used jquery to do the manipulation. Both methods can download pictures, but the picture downloaded by the jpegBtn2 method couldn't open correctly. Later I checked out the chrome console, I found that the href of jpegBtn2 remained "#", but the href of jpegBtn changed.I had no idea with this.
Here is my code:
if(!setDownloadDialogOrNot){
$('div.dialogBtnSet').append('<a class="jpegBtn2" id="jpegBtn2Id" type="button" href="#">close</a>');
$('div.dialogBtnSet').append('<a class="jpegBtn" type="button" href="#">jpeg</a>');
$('div.dialogBtnSet').append('<a id="pngBtn" type="button" href="#">png</a>');
setDownloadDialogOrNot=true;
};
$('.jpegBtn2').on('click',function(){
setDownloadCanvas(1);
$('.jpegBtn2').attr('download',filename+'.jpeg');
document.getElementById('jpegBtn2Id').href=document.getElementById('downloadCanvas').toDataURL('image/jpeg'));
$('div#downloadDialog').dialog('close');
});
$('.jpegBtn').on('click',function(){
setDownloadCanvas(1);
$('.jpegBtn').attr('download',filename+'.jpeg');
$('.jpegBtn').attr('href',document.getElementById('downloadCanvas').toDataURL('image/jpeg'));
$('div#downloadDialog').dialog('close');
});
$('#pngBtn').on('click',function(){
setDownloadCanvas(0);
$('#pngBtn').attr('download',filename+'.png');
$('#pngBtn').attr('href',document.getElementById('downloadCanvas').toDataURL('image/png'));
$('div#downloadDialog').dialog('close');
});
`
For binding events to elements added dynamically you need to use
$(initallyLoadedContainerElement).on('click', 'selector',function(){})
http://api.jquery.com/on/#direct-and-delegated-events
$(document).on('click', '.jpegBtn2',function(){
setDownloadCanvas(1);
$('.jpegBtn2').attr('download',filename+'.jpeg');
document.getElementById('jpegBtn2Id').href=document.getElementById('downloadCanvas').toDataURL('image/jpeg'));
$('div#downloadDialog').dialog('close');
});
$(document).on('click', '.jpegBtn',function(){
setDownloadCanvas(1);
$('.jpegBtn').attr('download',filename+'.jpeg');
$('.jpegBtn').attr('href',document.getElementById('downloadCanvas').toDataURL('image/jpeg'));
$('div#downloadDialog').dialog('close');
});
$(document).on('click', '#pngBtn',function(){
setDownloadCanvas(0);
$('#pngBtn').attr('download',filename+'.png');
$('#pngBtn').attr('href',document.getElementById('downloadCanvas').toDataURL('image/png'));
$('div#downloadDialog').dialog('close');
});
Related
I'm trying to use AJAX loading a set of sucessive pages in a main page, as I show you in the next picture:
I learned (thanks to this community!) to call other pages' content, by assigning the load() function to the onclick event of a button, like this:
$(document).ready(function() {
$('#btn').click(function() {
$('#result').load('./poi-data-no-heading.html');
});
});
But what if I have one button with id="btn" on every page? The functionality of any button with that id will be the same always, because (I think) the document.ready is not triggered when I use the load() method, so it's never replaced with new functionality.
E.g. initial functionality should be navigate from page 1 to page 2, and when page 2 is loaded, the functionality should be to navigate from page 2 to page 3.
As Js developer, I would do the following:
<!-- In the HTML file -->
<button id="btn" onclick="loadContent()">Load</button>
<div id="result"></div>
/* In the JS file */
function loadContent(){
/*the code to retrieve content*/
$('#result').load('http://fiddle.jshell.net/webdevem/JfcJp/show/');
}
This way I could assign the functionality to every button, no matter what's the ID or if the document.ready is triggered. But mixing Js with JQuery is not an option... So, how do you think I should manage to do something similar with JQuery?
Thanks in advance.
P/d: Here is a useful fiddle I used to try ideas: http://jsfiddle.net/gal007/vkcug7t7/1/
You could use the on() event from jQuery, which can listen for events on elements dynamically rendered (you can't do that with the click() method). So in this case you have to listen to the event on a parent element, one that doesn't change with the load method. On that button, use an HTML5 data-* attribute to define the id that you wish to load.
HTML:
<btn id="result" data-load-id="1">Load</btn>
Javascript:
$('#container').on('click', '#result', function() {
var id_to_load = $(this).data('load-id');
load('/url?' + id_to_load);
});
I've updated your fiddle : jsfiddle
I want hash-tags to be removed from URL after they are used.
For example, when i click on the link below:
<button type="button" name="" value="" id="btnq1">Just a button</button>
I want the hash-tag #btnq1 that appears to the URL of the page to be removed just after the action on this link happens.
I tried the below jquery code with no success:
$('#btnq1').click(function(event){
event.preventDefault();
// your action
});
And even if this works, then how do i implement it to work for every hash tag that is added to the URL?
I would like to solve it using javascript.
You could try that:
$(window).on('hashchange', function(e){
history.replaceState ("", document.title, e.originalEvent.oldURL);
});
first add a class to your a tag that you want this behavior for, or a html 5 data- attribute. Then your link becomes;
<button>Button</button>
$('body').on('click', ".remove-hash", function(e){
$(this).removeAttr('href');
});
I'm not really a developper. I prefer to design my websites ... So, for my actual project, i must developping some "basic" scripts.
I've met a problem with this:
<script type="text/javascript">
$("button").click(function toggleDiv(divId) {
$("#"+divId).toggle();
});
;
</script>
Into Head-/Head
LINK
<div> id="myContent">Lorem Ipsum</div>
It works for IE8. (Miracle). But not the others browsers...
The idea is that when u click on "LINK" a windows appears and when you click again, the window close.
Any idea ?
Thanks for u time !
One of the problems is you're mixing two different styles of binding event handlers: one of them is good (the jQuery method), the other is bad (the javascript: protocol in your href attribute) - the two don't work together in any way. Another problem is that your selector is completely incorrect (it's looking for a button) for the HTML you've provided (you never create a button).
I'd suggest using a HTML5 data-* attribute to specify the id for the <div> on your <a> element:
LINK
<div id="mycontent">Lorem ipsum</div>
Then use the following jQuery code:
$('a').click(function(e) {
e.preventDefault(); // e refers to the event (the click),
// calling preventDefault() will stop you following the link
var divId = $(this).data('divid');
$('#' + divId).toggle();
});
Note that I've used this in the above code; what this refers to depends on the context in which you use it, but in the context of a jQuery event handler callback function, it will always refer to the element that triggered the event (in this case, your <a> element).
If you extract toggleDiv from the handler, it ought to work. You will probably also need to return false to keep the href from trying to go anywhere.
<script type="text/javascript">
function toggleDiv(divId) {
$("#"+divId).toggle();
return false;
}
</script>
Simple quick question....
I have the following link html:
<a href="http://www.site.com/" onmouseover="" />
I have a javascript function which I want to enter some onmouseover information into that link dynamically. So, lets say it then becomes this for example if this javascript function is called:
<a href="http://www.site.com/" onmouseover="alert('howdy')" />
any ideas how to do this?
Add name attribute to and assign onmouseover
<a href="http://www.site.com/" onmouseover="" name="xxx"/>
document.getelementsbyname('xxx').onmouseover = function() { alert('howdy') }
Answer was, using setAttribute() javascript.
I think you want to say: dynamically change your href attribute information then you can do it by jquery
//Write code for prompt box and get value (when mouse-over)
$("a[href='http://www.google.com/']").attr('href', 'YOUR_GET_VALUE')
If you can use jquery, see: http://api.jquery.com/hover/
This is better than changing the attribute directly. Your javascript function can dynamically bind/unbind the mouse hover event and execute your alert call.
Otherwise your javascript function will need to dynamically change the attribute but you'll need to work around browser differences to locate the correct element then locate and modify the onmouseover attribute.
two options:
if it's something small:
<a href="http://www.site.com/" onmouseover="this.href = 'http://stackoverflow.com'" />
if you have something more to do:
<script type="text/javascript">
function doSomething(elem) {
elem.href = 'http://stackoverflow.com';
}
</script>
test
Or as stated before: use jQuery or any other framework to make your life a lot easier
The following works for jQuery every time
first the javascript:
$(document).on('mouseenter','.hovLink', function (e) {
e.preventDefault();
e.stopPropagation();
alert('entering ' + e.target.id);
}).on('mouseleave','.hovLink', function (e) {
alert('exiting ' + e.target.id);
});
and here is the HTML
Link
I am trying to add an onClick event to an anchor tag ...
Previously i had ...
<a href="somlink.html" onClick="pageTracker._link(this.href); return false;">
But i am trying to avoid the inline onClick event because it interferes with another script..
So using jQuery i am trying the following code ...
<script>
$(document).ready(function() {
$('a#tracked').attr('onClick').click(function() {window.onbeforeunload = null;
pageTracker._link(this.href);
return false;
});
});
</script>
with the html like so <a id="tracked" href="something.html">
So my question is should this be working, and if not what would be the best solution?
The correct way would be (as for jQuery)
$('#tracked').click(function() {
pageTracker._link($(this).attr('href'));
return false;
});
This will add an "onclick" event on any element with tracked id. There you can do anything you want. After the click event happens, the first line will pass href attribute of the clicked element to pageTracker.
As for your original question, it wouldnt work, it will raise undefined error. The attr works a bit different. See documentation . The way you used it, would return the value of the attribute and I think that in that case its not chainable. If you would like to keep it the way you had it, it should look like this:
$('#tracked').click(function() {
$(this).attr('onclick', 'pageTracker._link(this.href); return false;');
return false;
});
You can also try
var element1= document.getElementById("elementId");
and then
element1.setAttribute("onchange","functionNameAlreadyDefinedInYourScript()");
// here i am trying to set the onchange event of element1(a dropdown) to redirect to a function()
I spent some time on this yesterday. It turned out that I needed to include the jQuery on $(window).ready not $(document).ready.
$( window ).ready(function() {
$('#containerDiv a').click(function() {
dataLayer.push({
'event': 'trackEvent',
'gtmCategory': 'importantLinkSimilarProperties',
'gtmAction': 'Click',
'gtmLabel': $(this).attr('href')
});
});
});