Reputation: 4582
So i wanted to make some pretty buttons like http://www.oscaralexander.com/tutorials/how-to-make-sexy-buttons-with-css.html and everything is working fine. I wanted my forms to be able to be submitted with these sexy buttons.
So i started to use jQuery to handle all my "a" click actions.
$(document).ready(function() {
$("a").click(function() {
alert("a");
var a = $(this);
var id = a.attr("id");
switch (id) {
case "formSubmit":
a.parents("form:first").submit();
return false;
});
one more question... How do i get that code above to be highlighted in javascript? or any code like formating? Sorry for the crappy spaces, the second i find out how, i will edit it.
I got distracted and forgot to ask the original question. Now with jQuery it is easy to add in new information through $.post and other information. HOW do i add in a new a href link so that the $("a").click
catches it?
Upvotes: 0
Views: 73
Reputation: 65944
.click()
only binds your handler to already existing elements. To bind to any elements, whether they exist now or will exist later, you can use .live()
or .delegate()
. For the sake of simplicity, we'll use .live()
. The first parameter to .live()
is a string, the event you wish to bind your handler to. The second parameter is your handler function.
Just change what you have to something along these lines:
$(document).ready(function() {
$("a").live("click", function() {
alert("a");
var a = $(this);
var id = a.attr("id");
switch (id) {
case "formSubmit":
a.parents("form:first").submit();
return false;
});
Upvotes: 1
Reputation: 630379
Use a .live()
handler to handler clicks on new and future elements by replacing this:
$("a").click(function() {
With this:
$("a").live("click", function() {
This way a handler on document
listens for clicks from <a>
elements to bubble up...which happens for current and new elements.
Upvotes: 4