tags:

views:

349

answers:

5

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

+9  A: 

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.

John Boker
+1 - was just about to submit the same answer.
Buggabill
Thaks John that is a great help the only other thing is how do I pass a parameter to the function e.g if recordnumber 14 was clicked i need the open_dialog function to know it was initialised via record 14 and no other
Bazza
My suggestion would be to use the elements id for that. If you are building the page dynamically, each of elements can have a unique id assigned at the time they are created. Then, you can use $(this).attr("id") to refer to the id of the element that was clicked.
Buggabill
Similar to my answer here http://stackoverflow.com/questions/1794357/jquery-replacement-for-onclick/1794379#1794379
John Boker
-1 Custom attributes are not valid HTML, and he does not necessarily need a class here. Both of which add unnecessary bloat to the markup.
Josh Stodola
Major thanks to both John and Buggabill
Bazza
regarding @Josh Stodola's comment, it can be done like this but it is invalid markup, it can be done with classes. more info: http://www.danwebb.net/2007/10/7/custom-attributes-and-class-names
John Boker
A: 

you could use class instead of id's like:

$('.open_dialog').click(function(){ 
    $('#dialog').dialog("open");
    return false;
});
Reigel
all the element that has a class of 'open_dialog' will open a dialog box.
Reigel
Wouldn't work -- $(this) is going to refer to the link that was clicked, which is not the dialog he wants to open. You have to call .dialog on the ID which contains the dialog's content, in his case '#dialog'.
Parrots
A: 

You would us a class selector instead of an id selector.

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

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

In jQuery then you'd change what you have to this...

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

I'm confused as to what you're referencing with the "#dialog" id though. You didn't really explain what that is above... or at least I didn't get it if you did.

If there was any value from the recordset you're pulling that you needed to pass into the onclick function you could set the id of each anchor tag (assuming these values are unique) to be the value you need to pass, then reference them within the code like so... (I've stored the id of the link in a variable named rec_no below...

$('.openDialog').click(function(){
  var rec_no = $(this).attr("id");

  $("#dialog").dialog("open");
  return false;    
});
Ryan
A: 

Forget all of this class nonsense everyone else is talking about. Only use that if you absolutely have to (why add bloat to the markup if it is unnecessary?). Since you said your links are in a table, you can get all links within the table like this...

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

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

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

Get your record id inside the click event is easy too. Depends on which column it is in. This will alert the value of the first column in the table (within the click event)...

var id = $(this).parent().siblings("td:eq(0)").text();
alert(id);
Josh Stodola
A: 

I’m assuming the dialog function needs to know which record you’re referring to. You’ll need to output some sort of id for the record in your HTML. E.g.

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

Then amend the dialog function to accept the record’s id as an argument, and pass it in like this:

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

Is dialog one of your functions, or something built in to jQuery?

Paul D. Waite