Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - How do i call the same function with different element id tags?

Tags:

jquery

tags

I am a very new to jquery and am having trouble calling more than one instance of a function.

i have a function

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

To call this function i have an href link with an id tag name of open_dialog. Obviously this works great if it is the only link on the page referencing the function (A one to one relationship). However, I want (A many to one relationship).

I have a table of 25 records and I require each of my records to have a link which will call the open_dialog function I know that all the ids cannot be called open_dialog as they have to be unique, therefore how do I access the function while passing the value of which one of my 25 records is instantiating the function.

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

Thank you for looking at my post

like image 686
Bazza Avatar asked Jan 22 '10 14:01

Bazza


1 Answers

instead of using unique id's you can use a class on your items then just use

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

also, you can add another attribute to the anchor, ie

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

then inside your function you can have

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

record would now contain 14.

like image 93
John Boker Avatar answered Oct 16 '22 07:10

John Boker