views:

60

answers:

5

I am creating more than 1 <a href="#"></a> elements dynamically. The id attribute is also dynamically assigned. Like <a href="#" id='delete"+id+"'></a>. ID will becomes like delete01, delete02, ... I want to call a function when clicking any one of these element. I just know,

$("#someId").click(this.someFunction()); 

calls the function, when the element with the ID someId clicked.

The generated HTML looks like,

<a id="delete26" href="#" class="delete">Delete</a>
<a id="delete27" href="#" class="delete">Delete</a>
<a id="delete28" href="#" class="delete">Delete</a>

The JavaScript code that generates the above html looks like,

html += "<a class='delete' href='#' id='delete"+post.id+"'>Delete</a>";

Here, my ID's were dynamically generated. So, how shall i call a function when any one of the element got clicked.

Any suggestions!!!

Thanks!

+1  A: 

You could use the following selector:

$('a[id^=delete]').click(function() {
    var number = this.id.match(/^delete(\d+)$/)[1];
    return false;
});
Darin Dimitrov
Not sure why this is upvoted. OP said the markup is being added dynamically, so .live or event propogation must be used.
ScottE
@ScottE, once the elements are added to the DOM you can use the .click event handler, no matter how elements were added. .live is used if you intend to recreate those elements but such scenario wasn't mentioned by the OP.
Darin Dimitrov
A: 

use the live method and maybe a selector as $('a.delete') i assume the content is loaded through ajax, if not then your fine with $('a.delete')

Breezer
+2  A: 

You can use jQuery .live()

$(".delete").live("click", function() {
  // take some action
  return false;
});

And, if you want to call a named function you'd use the following. Note that I'm not using ()

$(".delete").live("click", this.someFunction)
ScottE
A: 

I have used this quite a lot. I don't know if it's the best solution, but try it anyways:

In your JavaScript have a function similar to this:

function doStuff(id)
{
    alert("doing things from ID: "+id);
}

Then with your hrefs do this (using '26' as example ID):

<a href="#" onclick="doStuff(26)" id="26">

It's probably crap but it works

JamWaffles
This question is tagged with jquery. No need for this obtrusive js.
ScottE
26 is also an invalid id
ScottE
@ScottE - You're right about the invalid ID in this answer, but the question is also tagged javascript and states *"Any suggestions!!!"*. This answer would work.
patrick dw
Just because something works doesn't mean it's a good answer. If the OP is already using jquery then this is a bad choice.
ScottE
@ScottE - I honestly don't know why this is bad. Is there some reason (other than it's not jQuery)?
patrick dw
Major advantages of jquery are the ability separate js functionality from the markup, and to write onobtrusive javascript. Couple of good points here http://www.smashingmagazine.com/2008/09/16/jquery-examples-and-best-practices/
ScottE
+5  A: 

The nicest way to do this is with .delegate(). This function uses a feature of Javascript called "event bubbling", which means that every time an event is fired on an element (such as click, focus, blur, mouseover) it is also fired on all that element's parent elements. You can therefore use a common ancestor element to trap all the events. The advantage of this is that you can add handlers for events that don't yet exist.

jQuery automates a lot of this for you:

$('#container').delegate('a.delete', 'click', this.someFunction);

This is very similar to live(), but in my opinion has nicer syntax and a slight performance increase.

Note that this example assumes that your links are all contained in an element with the id container.

lonesomeday
+1 I was just typing this answer. Sure seems as thought they share a common ancestor.
patrick dw
why do that when you could simple just type$('#container a.delete')???feels a little overkill if imo? no?assuming it has a parent element
Breezer
@Breezer Edited my answer to provide an answer for your question. Another advantage is that `delegate` does only one bind operation, rather than potentially dozens for normal binding, so is far quicker.
lonesomeday
@Breezer - I think you have it backwards. Using `.live()` is overkill because it needs to compare the `'#container a.delete'` selector agains *every* click on the page, instead of comparing `a.delete` against only those clicks inside `#container`.
patrick dw
@patrick dw and so i was told =)
Breezer
Looks like this was added in 1.4.2. Much easier than using event delegation, and faster than .live. Nice.
ScottE
@ScottE - This actually is event delegation (as is `.live()`) and could be accomplished prior to 1.4.2 by passing a context parameter, as in `$('a.delete','#container').live('click',func...`. Example: http://jsfiddle.net/DUt2s/ But using `.delegate()` now is much nicer.
patrick dw
@patrick - by event delegation I meant looking for the event target yourself, as in $("#container").click(function(e) { var $target = $(e.target)});
ScottE