This question already has answers here:
How do you override inline onclick event?
(4 answers)
Closed 8 years ago.
my html code is here.
mouse event functions writing like this
<li class="gnb1" onmouseover="fn1('param_01');" onmouseout="fn2('param_02','param_01');" > ...
I want change function and parameters programmatical as jquery or javascript.
$("li.gnb1").attr("onmouseover", "new_function_name()");
this code is not working. help me. show your move!
You can unbind the old handler and add a new one.
// To remove
$( "#foo").unbind( "click" );
// Then add new binding
$( "#foo").click(function () { });
Documentation for unbind is here: http://api.jquery.com/unbind/
[Edit as per comment]
If you have the ability to change the HTML, you could use jQuery to perform your bindings as well as your unbindings.
[Further edit]
You can however remove an inline event handler with removeAttr
$('#foo').removeAttr('onclick');
Related
This question already has answers here:
Event handler not working on dynamic content [duplicate]
(2 answers)
Closed 7 years ago.
$(".closeBtn").on('click',function(){
alert('');
});
+function myfunction(){
$(".elem").on('click',function(){
$("#someId").html('<img class="closeBtn" src=""/>'
);
});
}();
I used html to add some element into somewhere, I checked my dom and things are there. When I click the binded element closeBtn, it doesn't have respond. I thought using on() will work? or it's closure problem?
You need to use:
$("body").on('click','.closeBtn',function(){
alert('test');
});
Check Syntax For event delegation using on
This question already has answers here:
Firing event on DOM attribute change
(6 answers)
Closed 8 years ago.
I am trying to set up a listener to fire when an elements id changes, I don't know if this is possible but here is my attempt (that isn't working).
$(".appSelect").on('change', function() {
//fire change event
});
maybe something like :
$(".appSelect").attr("id").on('change', function() {
Is this possible in jquery?
Thanks!
It's not possible with a listener because no event occurs. You'd have to periodically check the ID with a setInterval function or similar. Better, tie into the function that changes the ID with a callback.
This question already has answers here:
How do I attach events to dynamic HTML elements with jQuery? [duplicate]
(8 answers)
Closed 8 years ago.
This is very common problem and i read a lot about this topic. But in my example, I came to the wall. And I ask you for help. How to bind functions using jQuery handler: .on();
$( "td" )
.on( "mouseenter", function() {
$( this ).css({
"background-color": "white"
});
Link to my example: http://jsfiddle.net/epredator/7Fz6X/
After click "Add person" button there append new row in my table. But this new element has no functionality like others. Hot to fix this, thanks for any hints!
The first selector needs to be something that already exists on the page, then you can delegate. For example:
$('body').on('mouseenter', 'td', function() {
This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 9 years ago.
I have a jquery calendar wich it takes events from a php file wich is feeding json data.
I want to bind a function on every event added to calendar. And I did this:
$(this).bind("mouseenter", function(){
alert("a");
});
It works, but only on second rollover on each event. First time I roll over nothing happens.
There are a couple of ways you can achieve this using JQuery.
$(selector).live( evt, function(){} ); // for JQuery 1.7 and less.
$(document).on( evt, selector, function(){} );
Event Delegation is yet another way you can use to achieve this.
consider this link => http://learn.jquery.com/events/event-delegation/
This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 9 years ago.
I am adding dynamic content to a php page using jQuery. I don't have a problem adding the content I just can't seem to find a way to make the new content respond to a click event.
For example (I'm using a div here but could be a button or other type).
$("#oldstuff").after('<div id="newstuff">Some content that should respond to click event</div>');
Edit:
I am currently using jquery 1.9.1, I was using 1.6.2 but I am checking out magnific-popup which required a newer jquery version.
I didn't post my failed attempts (
$("#newstuff").click(function() {...
$("#newstuff").live('click', function() {...
binding the click event, etc.
because I assumed that I am missing something fundamental that a more seasoned jQuery user would spot without the noise of broken code.
You can attach a click event before inserting the element into the DOM:
$('<div id="newstuff">...</div>').click(function(){
//do something
}).insertAfter('#oldstuff');
can't seem to find a way to make the new content respond to a click event.
You have to do event delegation
$("#oldstuff").on("click", '#newstuff',function(){
alert( $(this).text() );
});
What you need is to attach the event to the parent element.
HTML:
<div id="parent">
<div class="thediv">Hello</div>
</div>
JavaScript:
function addDiv() {
var div = document.createElement('div');
div.className = 'thediv';
document.getElementById('parent').appendChild(div);
}
$('#parent').on('click', '.thediv', function() {
// ...
});
See if this technique works.
It is probably you are binding event before attaching DOM and also in latest version of jquery library live method is deprecated so try to bind click even after attaching DOM or you can use following code...
first add DOM.
$("#oldstuff").after('<div id="newstuff">Some content that should respond to click event</div>');
and then bind event.
$('#newstuff').on('click',function(){alert('do your stuff!')});
Or
$('#newstuff').click(function(){alert('do your stuff!')});
Try this
$("#yourElementId").live("click",function(){
alert( $(this).text() );
});
As .click is not working after loading dom or dynamically created element