0

I would like to select a input field from a form on the fly, which mean, I don't know which form it would be. consider the following code: ( Suppose the input field id is always 'input1' across all forms)

$('[id^=myform]').submit(function(){
   var formId = $(this).attr('id');
   var result = $('#' + formId + ' input#inputl').val();
   ...
});

I am looking for a better solution for my purpose. Is there any?

2
  • 1
    Considering id has to be unique in a document, just #input1 would do the trick. Commented Oct 9, 2011 at 21:34
  • @muistooshort - Ahh, I see. Undeleted :) It was late and I was half asleep! Commented Oct 9, 2011 at 21:43

2 Answers 2

1

You'd be better off using the name attribute of the element (having multiple elements with the same ID is invalid in HTML), and making sure those names are kept consistent across forms:

$('[id^=myform]').submit(function(){
  var result = $(this).find('[name="email"]').val();
  ...
});
Sign up to request clarification or add additional context in comments.

Comments

0
$('[id^=myform]').submit(function(){
   var result = $(this).find('#input1').val();
});

Although like commented above. Ids are supposed to be unique across the entire page so you might need to change the id attribute into a name attribute for all of those input fields.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.