tags:

views:

61

answers:

3

I have couple of input fields with the class "link". All of them should start the jqueryUI dialog so this is why I bind the method to a class and not an single id. The difficulty is now that i can't use the (this) in line 12, because that gives me the identity of the dialog and not the input element.

As I am an beginner I don't know how to pass a variable to this event with the element of the input field.

What I want to archive is that the dialog should start from the input field and should write the result back to that input field.


   1.     // this is the click event for the input-field class called "link"
   2.     $('.link')
   3.         .button()
   4.         .click(function() {
   5.             $('#dialog-form').dialog('open');
   6.         
   7.         });  
   8.    
   9.     //this is an excerpt from the opened dialog box and the write back to the input field
  10.     $("#dialog-form").dialog({
  11.                     if (bValid) {
  12.                         $('.link').val('' +
  14.                             name.val() + '');
  15.                         $(this).dialog('close');
  16.                     }
  17.    });

A: 


$('.link').button().click(function() {
    $(this).addClass("selected-link");
    $('#dialog-form').dialog('open');
});


$("#dialog-form").dialog({
    if (bValid) {
        $('.link.selected-link').val('' + name.val() + '');
        $(this).dialog('close');
    }
});
mike clagg
Wow, jQuery can be so simple if you know some techniques. Thanks a lot, that works perfect.An advice for other beginners with the same problem. Remove the class, when you close the procedure with $(".selected-link").removeClass("selected-link");
twen_ta
+1  A: 
$('.link').button().click(function() {
  $('#dialog-form').data('clicked', $(this)).dialog('open');
});  

$('#dialog-form').dialog({
  if (bValid) {
    $('#dialog-form').data('clicked').val(name.val());
    $(this).dialog('close');
  }
});
Tgr
I also like that solution which appears very elegant to me. Didn't know that so many roads lead to Rome... Thanks!
twen_ta
A: 

the $('.link').something(), binds the event something to every DOM node with the class "link". You are mistaken on what you said. You can still use the $(this) as it refers to the activated DOM node and not the collection of classes that the event was associated to.

fmsf
Maybe I don't get the clue, but this: 12. $('.link').val('' +doesn't work. Maybe if I do the .val in the click function?
twen_ta