Jquery - How to call the same function with different id tags of an element?

I am very new to jquery and I am unable to call more than one function instance.

i has a function

$('#open_dialog').click(function(){ $("#dialog").dialog("open"); return false; }); 

To call this function, I have a href link with the id tag name open_dialog. Obviously, this works great if this is the only link on the page that references the function (one-to-one relationships). However, I want to (many-to-one relationship).

I have a table of 25 entries, and I require that each of my entries have a link that will call the open_dialog function, which I know that all identifiers cannot be called by open_dialog, since they must be unique, so how do I access function, passing the value that one of my 25 entries creates an instance of the function.

By the way, my entries are dynamic, so $ ('# open_dialog, open_dialog2, open_dialog3, ...') is not practical.

Thanks for watching my post

+6
jquery tags
source share
5 answers

instead of using a unique identifier you can use a class for your elements and then just use

 $('.classname').click(function() { // 'this' would reference the anchor that was clicked $("#dialog").dialog("open"); return false; }); 

also you can add one more attribute of binding, i.e.

 <a href="#" class="classname" record="14">Record 14</a> 

then inside your function you may have

 var record = $(this).attr("record"); 

record will now contain 14.

+11
source share

You can use the class instead of id:

 $('.open_dialog').click(function(){ $('#dialog').dialog("open"); return false; }); 
0
source share

Instead of the id selector, you would choose a class selector.

Apply the same class to each of your links ... let's say 'openDialog'

 <a class='openDialog' href='your link here'>Your text here</a> 

In jQuery, you change what you have on this ...

 $('.openDialog').click(function(){ $("#dialog").dialog("open"); return false; }); 

I am confused by the fact that you are referencing the identifier "#dialog". You really didnโ€™t explain that this is higher ... or at least I didnโ€™t understand this if you did.

If any value from the recordset that you pull out needs to be passed to the onclick function, you can set the identifier of each anchor tag (provided that these values โ€‹โ€‹are unique) to be the value to be passed, then refer to them inside the code as ... (I saved the link identifier in a variable named rec_no below ...

 $('.openDialog').click(function(){ var rec_no = $(this).attr("id"); $("#dialog").dialog("open"); return false; }); 
0
source share

Forget about all this stupid class that everyone else is talking about. Use only this if you absolutely need to (why add bloat to the markup if you don't need it?). Since you said that your links are in a table, you can get all the links in a table like this ...

 $("table a").click(function() { ... }); 

Or if your table has an identifier on it ...

 $("#tableId a").click(function() { ... }); 

Retrieving your post id inside the click event is easy. Depends on which column it is in. This will warn the value of the first column in the table (inside the click event) ...

 var id = $(this).parent().siblings("td:eq(0)").text(); alert(id); 
0
source share

Im suggesting that the dialog function should know which record you are referring to. You will need to output some sort of identifier for the entry in your HTML. For example.

 <a class="open_dialog" id="record1"></a> 

Then modify the dialog function to accept the identifier of the entries as an argument, and pass it as follows:

 $('.open_dialog').click(function(){ $("#dialog").dialog("open", $(this).id); return false; }); 

Is dialog one of your functions or is something embedded in jQuery?

0
source share

All Articles