jquery on not working properly - javascript

I have the following jquery code
$(".delete").on('click', function (e){
e.preventDefault();
var id = $(this).prop('id');
console.log('Delete button id: '+id);
var formWrapper = $(".form-wrapper:nth-child("+id+")");
console.log(formWrapper);
formWrapper.remove();
});
the delete .delete is on a button inside a form
<button class="btn btn-danger delete">-<button>
and the button is loaded on the paged ynamically after the page has loaded. So I used the on function to attach the click event on it. But it won't work and the function is never called. Isn't on supposed to work not only for elements that are on the page during load but for those that get loaded afterwards?

You are saying that the particular button is getting loaded to the DOM dynamically, so in this context you have to use event-delegation to make your code working.
Normally your code will register event for the element with the class .delete immediately after the DOM loaded. Actually we dont have any elements with that identity at that time, So this context is opt for event-delegation.
I actually dont know the exact dom structure of yours, so that i have used document to delegate the click event, But you should prefer some static parent of that element to implement it,
Try,
$(document).on("click",".delete", function (e){

You need to use event delegation for dynamically generated elements. thus use .on() using delegated-events approach.
i.e.
$(document).on('event','selector',callback_function)
Use
$(document).on('click',".delete", function (e){
In place of document you should use closest static container.
The delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, we can use delegated events to bind the click event to dynamically created elements and also to avoid the need to frequently attach and remove event handlers.

you should either use,
$(document).on('click',".delete", function (){});
or
$(".delete").click(function(){});

You can try jquery delegate() Method.
In jquery version 7 there was a term live however in latest one its removed and replace with delegate
you can check below example
HTML
< div class="delete"> Click Here < /div >
Javascript
$(document).delegate( ".delete", "click", function() {
alert("Hi")
});

This might help: http://jsfiddle.net/webcarvers/7Qtd7/
HTML:
<button id="one" class="delete"type="button">Id on me</button>
<div id="one">This is the div to remove</div>
JS:
$(document).ready(function(){
$("button.delete").on('click', function(){
var id = $(this).attr("id");
$("div#"+id).remove();
});
});

Related

jQuery onclick event not fire [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 5 years ago.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function() {
$("h2").html("<p class='test'>click me</p>")
});
$(".test").click(function(){
alert();
});
});
</script>
</head>
<body>
<h2></h2>
<button>generate new element</button>
</body>
</html>
I was trying to generate a new tag with class name test in the <h2> by clicking the button. I also defined a click event associated with test. But the event doesn't work.
Can anyone help?
The click() binding you're using is called a "direct" binding which will only attach the handler to elements that already exist. It won't get bound to elements created in the future. To do that, you'll have to create a "delegated" binding by using on().
Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.
Source
Here's what you're looking for:
var counter = 0;
$("button").click(function() {
$("h2").append("<p class='test'>click me " + (++counter) + "</p>")
});
// With on():
$("h2").on("click", "p.test", function(){
alert($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<h2></h2>
<button>generate new element</button>
The above works for those using jQuery version 1.7+. If you're using an older version, refer to the previous answer below.
Previous Answer:
Try using live():
$("button").click(function(){
$("h2").html("<p class='test'>click me</p>")
});
$(".test").live('click', function(){
alert('you clicked me!');
});
Worked for me. Tried it with jsFiddle.
Or there's a new-fangled way of doing it with delegate():
$("h2").delegate("p", "click", function(){
alert('you clicked me again!');
});
An updated jsFiddle.
Use the .on() method with delegated events
$('#staticParent').on('click', '.dynamicElement', function() {
// Do something on an existent or future .dynamicElement
});
The .on() method allows you to delegate any desired event handler to:
current elements or future elements added to the DOM at a later time.
P.S: Don't use .live()! From jQuery 1.7+ the .live() method is deprecated.
Reason:
In jQuery, Click() event Direct binding which attaches the event handler to the element only if the particular element(Html code) exists on the page(after page loads).
Dynamic elements are created with the help of javascript or jquery(not in Html).
It won't consider the future elements(Dynamic) which are created after the page gets loaded.
So the normal click event won't fire on the dynamic element.
Solution :
To overcome this, we should use on() function. on() can delegate the event for both the current and future elements.
Delegated events have the advantage that can attach the handler to the elements which are being added to the document in the future.
Note : delegate(),live() and on() functions have the advantages over the DOM elements. As of JQuery 1.7 delegate() and live() are deprecated(Don't use these).
on() only Can delegate the event for both current and future elements.
So, Your code should be like this
Remove the code from $(document).ready:
$(".test").click(function(){
alert();
});
Change into:
$(document).on('click','.test',function(){
alert('Clicked');
});
Add this function in your js file.
It will work on every browser
$(function() {
$(document).on("click", '#mydiv', function() {
alert("You have just clicked on ");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='mydiv'>Div</div>
Change
$(".test").click(function(){
To
$(".test").live('click', function(){
LIVE DEMO
jQuery .live()
You need to use .live for this to work:
$(".test").live("click", function(){
alert();
});
or if you're using jquery 1.7+ use .on:
$(".test").on("click", "p", function(){
alert();
});
Try .live() or .delegate()
http://api.jquery.com/live/
http://api.jquery.com/delegate/
Your .test element was added after the .click() method, so it didn't have the event attached to it. Live and Delegate give that event trigger to parent elements which check their children, so anything added afterwards still works. I think Live will check the entire document body, while Delegate can be given to an element, so Delegate is more efficient.
More info:
http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/
I found two solutions at the jQuery's documentation:
First: Use delegate on Body or Document
E.g:
$("body").delegate('.test', 'click', function(){
...
alert('test');
});
Why?
Answer: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.
link: http://api.jquery.com/delegate/
Second: Put the your function at the "$( document )", using "on" and attach it to the element that you want to trigger this.
The first parameter is the "event handler", the second: the element and the third: the function.
E.g:
$( document ).on( 'click', '.test', function () {
...
alert('test');
});
Why?
Answer: Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next ...
link: https://api.jquery.com/on/
Best way to apply event on dynamically generated content by using delegation.
$(document).on("eventname","selector",function(){
// code goes here
});
so your code is like this now
$(document).on("click",".test",function(){
// code goes here
});
$(.surrounding_div_class).on( 'click', '.test', function () {
alert( 'WORKS!' );
});
Will only work if the DIV with the class .surrounding_div_class is the immediate parent to the object .test
If there is another object in the div that will be filled it wont work.
The problem you have is that you're attempting to bind the "test" class to the event before there is anything with a "test" class in the DOM. Although it may seem like this is all dynamic, what is really happening is JQuery makes a pass over the DOM and wires up the click event when the ready() function fired, which happens before you created the "Click Me" in your button event.
By adding the "test" Click event to the "button" click handler it will wire it up after the correct element exists in the DOM.
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("h2").html("<p class='test'>click me</p>")
$(".test").click(function(){
alert()
});
});
});
</script>
Using live() (as others have pointed out) is another way to do this but I felt it was also a good idea to point out the minor error in your JS code. What you wrote wasn't wrong, it just needed to be correctly scoped. Grasping how the DOM and JS works is one of the tricky things for many traditional developers to wrap their head around.
live() is a cleaner way to handle this and in most cases is the correct way to go. It essentially is watching the DOM and re-wiring things whenever the elements within it change.
An alternate and more succinct alternative (IMHO) is to use a raw javascript function that responds to an on click event, then pass the target element back to jQuery if you like. The advantage of this approach is that you can dynamically add your element anywhere, and the click handler will 'just work', and you need not concern yourself with delegating control to parent elements, and so on.
Step 1: Update the dynamic html to fire an onclick event. Be sure to pass the 'event' object as an argument
$("button").click(function() {
$("h2").html("<p class='test' onclick='test(event)'> click me </p>")
});
Step 2: Create the test function to respond to the click event
function test(e){
alert();
});
Optional Step 3: Given you are using jQuery I'm assuming it will be useful to get a reference back to the source button
function test(e){
alert();
// Get a reference to the button
// An explanation of this line is available here
var target = (e.target)? e.target : e.srcElement;
// Pass the button reference to jQuery to do jQuery magic
var $btn = $(target);
});
.live function works great.
It is for Dynamically added elements to the stage.
$('#selectAllAssetTypes').live('click', function(event){
alert("BUTTON CLICKED");
$('.assetTypeCheckBox').attr('checked', true);
});
Cheers,
Ankit.
The Jquery .on works ok but I had some problems with the rendering implementing some of the solutions above. My problem using the .on is that somehow it was rendering the events differently than the .hover method.
Just fyi for anyone else that may also have the problem. I solved my problem by re-registering the hover event for the dynamically added item:
re-register the hover event because hover doesn't work for dynamically created items.
so every time i create the new/dynamic item i add the hover code again. works perfectly
$('#someID div:last').hover(
function() {
//...
},
function() {
//...
}
);
I'm working with tables adding new elements dynamically to them, and when using on(), the only way of making it works for me is using a non-dynamic parent as:
<table id="myTable">
<tr>
<td></td> // Dynamically created
<td></td> // Dynamically created
<td></td> // Dynamically created
</tr>
</table>
<input id="myButton" type="button" value="Push me!">
<script>
$('#myButton').click(function() {
$('#myTable tr').append('<td></td>');
});
$('#myTable').on('click', 'td', function() {
// Your amazing code here!
});
</script>
This is really useful because, to remove events bound with on(), you can use off(), and to use events once, you can use one().
I couldn't get live or delegate to work on a div in a lightbox (tinybox).
I used setTimeout successfullly, in the following simple way:
$('#displayContact').click(function() {
TINY.box.show({html:'<form><textarea id="contactText"></textarea><div id="contactSubmit">Submit</div></form>', close:true});
setTimeout(setContactClick, 1000);
})
function setContactClick() {
$('#contactSubmit').click(function() {
alert($('#contactText').val());
})
}
Also you can use onclick="do_something(this)"inside element
If you have a dinamically added link to some container or the body:
var newLink= $("<a></a>", {
"id": "approve-ctrl",
"href": "#approve",
"class": "status-ctrl",
"data-attributes": "DATA"
}).html("Its ok").appendTo(document.body);
you can take its raw javascript element and add an event listener to it, like the click:
newLink.get(0).addEventListener("click", doActionFunction);
No matter how many times you add this new link instance you can use it as if you where using a jquery click function.
function doActionFunction(e) {
e.preventDefault();
e.stopPropagation();
alert($(this).html());
}
So you will receive a message saying
Its ok
It has better performance than other alternatives.
Extra: You could gain better performance avoiding jquery and using plain javascript. If you are using IE up to version 8 you should use this polyfill to use the method addEventListener
if (typeof Element.prototype.addEventListener === 'undefined') {
Element.prototype.addEventListener = function (e, callback) {
e = 'on' + e;
return this.attachEvent(e, callback);
};
}
You CAN add on click to dynamically created elements. Example below. Using a When to make sure its done. In my example, i'm grabbing a div with the class expand, adding a "click to see more" span, then using that span to hide/show the original div.
$.when($(".expand").before("<span class='clickActivate'>Click to see more</span>")).then(function(){
$(".clickActivate").click(function(){
$(this).next().toggle();
})
});
Use 'on' as click gets bind to the elements already present.
For e.g
$('test').on('click',function(){
alert('Test');
})
This will help.

how to get element value where multiple class name is same in jquery [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 5 years ago.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function() {
$("h2").html("<p class='test'>click me</p>")
});
$(".test").click(function(){
alert();
});
});
</script>
</head>
<body>
<h2></h2>
<button>generate new element</button>
</body>
</html>
I was trying to generate a new tag with class name test in the <h2> by clicking the button. I also defined a click event associated with test. But the event doesn't work.
Can anyone help?
The click() binding you're using is called a "direct" binding which will only attach the handler to elements that already exist. It won't get bound to elements created in the future. To do that, you'll have to create a "delegated" binding by using on().
Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time.
Source
Here's what you're looking for:
var counter = 0;
$("button").click(function() {
$("h2").append("<p class='test'>click me " + (++counter) + "</p>")
});
// With on():
$("h2").on("click", "p.test", function(){
alert($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<h2></h2>
<button>generate new element</button>
The above works for those using jQuery version 1.7+. If you're using an older version, refer to the previous answer below.
Previous Answer:
Try using live():
$("button").click(function(){
$("h2").html("<p class='test'>click me</p>")
});
$(".test").live('click', function(){
alert('you clicked me!');
});
Worked for me. Tried it with jsFiddle.
Or there's a new-fangled way of doing it with delegate():
$("h2").delegate("p", "click", function(){
alert('you clicked me again!');
});
An updated jsFiddle.
Use the .on() method with delegated events
$('#staticParent').on('click', '.dynamicElement', function() {
// Do something on an existent or future .dynamicElement
});
The .on() method allows you to delegate any desired event handler to:
current elements or future elements added to the DOM at a later time.
P.S: Don't use .live()! From jQuery 1.7+ the .live() method is deprecated.
Reason:
In jQuery, Click() event Direct binding which attaches the event handler to the element only if the particular element(Html code) exists on the page(after page loads).
Dynamic elements are created with the help of javascript or jquery(not in Html).
It won't consider the future elements(Dynamic) which are created after the page gets loaded.
So the normal click event won't fire on the dynamic element.
Solution :
To overcome this, we should use on() function. on() can delegate the event for both the current and future elements.
Delegated events have the advantage that can attach the handler to the elements which are being added to the document in the future.
Note : delegate(),live() and on() functions have the advantages over the DOM elements. As of JQuery 1.7 delegate() and live() are deprecated(Don't use these).
on() only Can delegate the event for both current and future elements.
So, Your code should be like this
Remove the code from $(document).ready:
$(".test").click(function(){
alert();
});
Change into:
$(document).on('click','.test',function(){
alert('Clicked');
});
Add this function in your js file.
It will work on every browser
$(function() {
$(document).on("click", '#mydiv', function() {
alert("You have just clicked on ");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='mydiv'>Div</div>
Change
$(".test").click(function(){
To
$(".test").live('click', function(){
LIVE DEMO
jQuery .live()
You need to use .live for this to work:
$(".test").live("click", function(){
alert();
});
or if you're using jquery 1.7+ use .on:
$(".test").on("click", "p", function(){
alert();
});
Try .live() or .delegate()
http://api.jquery.com/live/
http://api.jquery.com/delegate/
Your .test element was added after the .click() method, so it didn't have the event attached to it. Live and Delegate give that event trigger to parent elements which check their children, so anything added afterwards still works. I think Live will check the entire document body, while Delegate can be given to an element, so Delegate is more efficient.
More info:
http://www.alfajango.com/blog/the-difference-between-jquerys-bind-live-and-delegate/
I found two solutions at the jQuery's documentation:
First: Use delegate on Body or Document
E.g:
$("body").delegate('.test', 'click', function(){
...
alert('test');
});
Why?
Answer: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.
link: http://api.jquery.com/delegate/
Second: Put the your function at the "$( document )", using "on" and attach it to the element that you want to trigger this.
The first parameter is the "event handler", the second: the element and the third: the function.
E.g:
$( document ).on( 'click', '.test', function () {
...
alert('test');
});
Why?
Answer: Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next ...
link: https://api.jquery.com/on/
Best way to apply event on dynamically generated content by using delegation.
$(document).on("eventname","selector",function(){
// code goes here
});
so your code is like this now
$(document).on("click",".test",function(){
// code goes here
});
$(.surrounding_div_class).on( 'click', '.test', function () {
alert( 'WORKS!' );
});
Will only work if the DIV with the class .surrounding_div_class is the immediate parent to the object .test
If there is another object in the div that will be filled it wont work.
The problem you have is that you're attempting to bind the "test" class to the event before there is anything with a "test" class in the DOM. Although it may seem like this is all dynamic, what is really happening is JQuery makes a pass over the DOM and wires up the click event when the ready() function fired, which happens before you created the "Click Me" in your button event.
By adding the "test" Click event to the "button" click handler it will wire it up after the correct element exists in the DOM.
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("h2").html("<p class='test'>click me</p>")
$(".test").click(function(){
alert()
});
});
});
</script>
Using live() (as others have pointed out) is another way to do this but I felt it was also a good idea to point out the minor error in your JS code. What you wrote wasn't wrong, it just needed to be correctly scoped. Grasping how the DOM and JS works is one of the tricky things for many traditional developers to wrap their head around.
live() is a cleaner way to handle this and in most cases is the correct way to go. It essentially is watching the DOM and re-wiring things whenever the elements within it change.
An alternate and more succinct alternative (IMHO) is to use a raw javascript function that responds to an on click event, then pass the target element back to jQuery if you like. The advantage of this approach is that you can dynamically add your element anywhere, and the click handler will 'just work', and you need not concern yourself with delegating control to parent elements, and so on.
Step 1: Update the dynamic html to fire an onclick event. Be sure to pass the 'event' object as an argument
$("button").click(function() {
$("h2").html("<p class='test' onclick='test(event)'> click me </p>")
});
Step 2: Create the test function to respond to the click event
function test(e){
alert();
});
Optional Step 3: Given you are using jQuery I'm assuming it will be useful to get a reference back to the source button
function test(e){
alert();
// Get a reference to the button
// An explanation of this line is available here
var target = (e.target)? e.target : e.srcElement;
// Pass the button reference to jQuery to do jQuery magic
var $btn = $(target);
});
.live function works great.
It is for Dynamically added elements to the stage.
$('#selectAllAssetTypes').live('click', function(event){
alert("BUTTON CLICKED");
$('.assetTypeCheckBox').attr('checked', true);
});
Cheers,
Ankit.
The Jquery .on works ok but I had some problems with the rendering implementing some of the solutions above. My problem using the .on is that somehow it was rendering the events differently than the .hover method.
Just fyi for anyone else that may also have the problem. I solved my problem by re-registering the hover event for the dynamically added item:
re-register the hover event because hover doesn't work for dynamically created items.
so every time i create the new/dynamic item i add the hover code again. works perfectly
$('#someID div:last').hover(
function() {
//...
},
function() {
//...
}
);
I'm working with tables adding new elements dynamically to them, and when using on(), the only way of making it works for me is using a non-dynamic parent as:
<table id="myTable">
<tr>
<td></td> // Dynamically created
<td></td> // Dynamically created
<td></td> // Dynamically created
</tr>
</table>
<input id="myButton" type="button" value="Push me!">
<script>
$('#myButton').click(function() {
$('#myTable tr').append('<td></td>');
});
$('#myTable').on('click', 'td', function() {
// Your amazing code here!
});
</script>
This is really useful because, to remove events bound with on(), you can use off(), and to use events once, you can use one().
I couldn't get live or delegate to work on a div in a lightbox (tinybox).
I used setTimeout successfullly, in the following simple way:
$('#displayContact').click(function() {
TINY.box.show({html:'<form><textarea id="contactText"></textarea><div id="contactSubmit">Submit</div></form>', close:true});
setTimeout(setContactClick, 1000);
})
function setContactClick() {
$('#contactSubmit').click(function() {
alert($('#contactText').val());
})
}
Also you can use onclick="do_something(this)"inside element
If you have a dinamically added link to some container or the body:
var newLink= $("<a></a>", {
"id": "approve-ctrl",
"href": "#approve",
"class": "status-ctrl",
"data-attributes": "DATA"
}).html("Its ok").appendTo(document.body);
you can take its raw javascript element and add an event listener to it, like the click:
newLink.get(0).addEventListener("click", doActionFunction);
No matter how many times you add this new link instance you can use it as if you where using a jquery click function.
function doActionFunction(e) {
e.preventDefault();
e.stopPropagation();
alert($(this).html());
}
So you will receive a message saying
Its ok
It has better performance than other alternatives.
Extra: You could gain better performance avoiding jquery and using plain javascript. If you are using IE up to version 8 you should use this polyfill to use the method addEventListener
if (typeof Element.prototype.addEventListener === 'undefined') {
Element.prototype.addEventListener = function (e, callback) {
e = 'on' + e;
return this.attachEvent(e, callback);
};
}
You CAN add on click to dynamically created elements. Example below. Using a When to make sure its done. In my example, i'm grabbing a div with the class expand, adding a "click to see more" span, then using that span to hide/show the original div.
$.when($(".expand").before("<span class='clickActivate'>Click to see more</span>")).then(function(){
$(".clickActivate").click(function(){
$(this).next().toggle();
})
});
Use 'on' as click gets bind to the elements already present.
For e.g
$('test').on('click',function(){
alert('Test');
})
This will help.

jquery dynamic button click not working

I am generating some buttons dynamically with dynamic ids. I wish to handle each button's click based on its id. But that's not working.
Following is my code:
var del_id = "del"+countPage+"-"+questionCount;
html+='<tr><td><input type="button" value="delete" id="'+del_id+'" /><td><input type="button" value="Copy" id="copy_id"/></tr>';
$("#"+del_id).click(function(){
alert("jkhkjh");
});
If I give static id instead of del_id, then it works. Where am I getting wrong? How do I solve this?
edited:solved
html+='<tr><td id="'+del_id+'"><input type="button" value="delete" id="'+del_id+'" /><td><input type="button" value="Copy" id="copy_id"/></tr>';
$(document).on("click", "#"+del_id, function(){
alert("jkhkjh");
});
You have to use Event-delegation to hook the events with elements which are loaded dynamically.
Try,
$(document).on("click", "#"+del_id, function(){
Or try like this, dont register events for each and every buttons since they are performing the same task.
$(document).on("click", "[type='button'][id^='del']", function(){
Or add a common class to that button element and try like this,
$(document).on("click", ".buttonClass", function(){
Try this for dynamically created buttons
$(document).on('click', 'buttonId/Class', function(){
// Do something.
})
Use
$(document).on("click", "#"+del_id, function(){
});
You are giving the same id to 2 different DOM elements (TD and input). Id's are supposed to be unique.
jQuery then gets only the first DOM element that it finds.
**.live()** -
This method provides a means to attach delegated event handlers to the document element of a page, which simplifies the use of event handlers when content is dynamically added to a page.
$( "#dymanic" ).live( "click", function() {
alert( "Goodbye!" ); // jQuery 1.3+
});
Refer to this documentation
https://api.jquery.com/live/
if your controls are being added dynamically on page, they should be handled with special events that take future events into account. like on() or delegate().
I will prefer delegate() as I used it numerous times.
$('body').on("#"+del_id, 'click', function(){
//Do something.
});
Note: with delegate() or on() you have an advantage of attaching multiple events handlers and make your selector search faster as you can specify the parent element to search within. In this case I mentioned body as parent selector. you can specify a more closer parent node.

How to add onclick to Div with a specific class name?

I have a few generated div's on my page listing events on a calender, they all have the same class "fc-event-inner". I would like to add a onclick to these div's but am struggling to get this right.
This is what iv tried, no onclick is added and no errors on page.
$(document).ready(function () {
$('.fc-event-inner').each(
function (element) {
Event.observe("click", element, EventClick);
}
);
function EventClick() {
alert("You clicked an event")
}
});
This is an example of a generated event div:
<div class="fc-event-inner">
<span class="fc-event-title">Requested<br>by Santa</span>
</div>
Use the delegate version of on
$(document).on("click", ".fc-event-inner", function(){
/// do your stuff here
});
This catches the click at the document level then applies the class filter to see if the item clicked is relevant.
Example JSFiddle: http://jsfiddle.net/BkRJ2/
In answer to comment:
You can access the clicked element via this inside the event function. e.g.
$(document).on("click", ".fc-event-inner", function(){
var id = this.id; // Get the DOM element id (if it has one)
var $this = $(this); // Convert DOM element into a jQuery object to do cool stuff
$this.css({'background-color': 'red'}); // e.g. Turn clicked element red
});
*Note: You should never have to run an Each in order to catch events on multiple items that have a common class.
You do not need each() to bind event to elements with specific class, just selector is enough. Use jQuery on() with event delegation it will bind event to those which are generted after the binding code.
$(document).on("click", ".fc-event-inner", function(){
alert("click");
});
Delegated events
Delegated events have the advantage that they can process events from
descendant elements that are added to the document at a later time. By
picking an element that is guaranteed to be present at the time the
delegated event handler is attached, you can use delegated events to
avoid the need to frequently attach and remove event handlers, jQuery doc.
<div class="fc-event-inner">
<span class="fc-event-title">Requested<br />by Santa</span>
</div>
Your JS:
<script>
$(document).ready(function () {
$('.fc-event-inner').on("click", EventClick);
function EventClick() {
alert("You clicked an event")
}
});
</script>
http://jsfiddle.net/UBhk9/
Some explanation:
Because you are using a class(it may be used multiple times, in contrast to an id) it will work for all the elements with this class name. The .on method will attach the event handler(in this example "click") to the selector(the class .fc-event-inner). If you want to remove events bounds you've to use the .off() method and if you only want to attach the event once you can use the .one() method.

Click not working after clearing and appending the html

click not working after i clear and append the html inside a div using jquery.
Here is the html code
<div id="divMain">
</div>
<input id="btn" type="button" value="Clear&Add"/>
Here is the jQuery code
var a = $('<a/>').attr({'id':'aH','href':'#'}).text('Hello');
a.click(function(){
alert('hello');
});
$('#divMain').append(a);
$('#btn').click(function(){
var newA = $('#aH');
$('#divMain').html('');
$('#divMain').append(newA);
});
Here is jsfiddle
Simple click on the alert link in fiddle , it shows an alert.Now click on the Clear&Add button .And now click on alert.It doesn't work.
You need event delegation to bind the event with dynamically added elements. You also need to create elemet with id aH as you have removed the element from DOM without preserving it.
Live Demo
$(document).on('click', '#aH', function(){
alert('hello');
});
You can try adding the globally created a and you would not need to bind click again.
$('#divMain').append(a);
Delegated events have the advantage that they can process events from
descendant elements that are added to the document at a later time. By
picking an element that is guaranteed to be present at the time the
delegated event handler is attached, you can use delegated events to
avoid the need to frequently attach and remove event handlers, jQuery api
You need to use event delegation:
$('#divMain').on('click', a, function(){
alert('hello');
});
Updated Fiddle: http://jsfiddle.net/W95wV/1/
Click does not work on dynamically created elements in jQuery. In the earlier version of jQuery we could use .live('click') that has been deprecated in the recent versions. Now you can use .on to work better for dynamic elements as Adil said.

Categories

Resources