0

I am trying to get the name's value from an input HTML element using jQuery but it doesn't works.

HTML code:

<p class="myclass"><input type="text" name="thisismyname1" value="123"></p>
<p class="myclass"><input type="text" name="thisismyname2" value="456"></p>
<p class="myclass"><input type="text" name="thisismyname3" value="789"></p>

I am trying to get all the name values using this peace of code in jQuery:

$(.myclass).each(function(value){
  alert('input name='+ $(this).attr('name') +'='+$(this).val());
  });

This $(this).attr('name') seems not to be working.

2 Answers 2

2

You search by p but p does not have attribute name, your selector should like this

$('.myclass input').each(function(value){
  console.log('input name='+ $(this).attr('name') +'='+$(this).val());
});

Demo: http://jsbin.com/jujaho/2/edit

Sign up to request clarification or add additional context in comments.

Comments

0

You need to look for the input under the context of each iteration, like below,

$(".myclass").each(function() {

  var $input = $(this).find("input");
  alert ("Name: " + $input.attr("name"));
  alert ("Value: " + $input.val());
});

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.