views:

71

answers:

2

I'm not sure if I have the syntax correct in the code below, I'm trying to append a var to a string parameter within the find function. I'm trying to search for a unique id within each input element of a particular form.

 //Get value attribute from submit button
 var name = $('#myForm').find('input#submitThis').val();

    //Other code that manipulates the name variable

 //Submit button in hidden form
 $('.submitLink').click(function(){
  $('#myForm').find('input#'+name).click();
  return false;
 });

The element with a submitLink class is supposed to be tied to the submit button in the form. I don't think I have the syntax correct though, when I go back and click the element that has the submitLink class, nothing happens.

A: 

Try adding an alert to test the var inside the event handler (and to see that the handler is fired). Also, if you are looking for an element with a specific id you don't need to include the element type. Like this:

$('.submitLink').click(function (event) {
  event.preventDefault();
  alert(name);
  $('#' + name, $('#myForm')).click();
});

NOTE: If you are trying to find an element by its name rather than ID you must use $("input[name='foo']").

David Radcliffe
Thank you both very much David and ayaz. Appreciate your help, this was exactly what I was looking for. I was using the wrong attribute, I had the name, when instead I meant to use the id. Thanks again.
kingrichard2005
A: 

The syntax appears fine to me. To be sure the selector is what you are expecting it to be, you could do something like this:

$('.submitLink').click(function() {
   var selector = 'input#' + name;
   alert(selector);
   /* rest of the code */
});
ayaz