0

I'm total beginner in jquery.

I tried this https://jsfiddle.net/0xLqhufd/2/

<body>
<div>
<input type="text" person="firstName" value="John">
<input type="text" person="birthDate" value="September 1st, 2000">
</div>
</body>

But I got only blank in alert :

$('input[person]').each(function(index) {
    alert(index + ': ' + $(this).text());
  }
);
1
  • 1
    Instead of creating your own custom tags use html5 data-* attributes. Commented Apr 4, 2016 at 11:55

5 Answers 5

2

As said on the jQuery documentation ...

The .text() method cannot be used on form inputs or scripts

Use .val() instead.

$('input[person]').each(function(index) {
    alert(index + ': ' + $(this).val());
  }
);

Working Demo

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

1 Comment

It is always good to include a working example in your answer. I have included it.
1

In void elements, using text() would be inappropriate. Use .val() instead. input[type=text] is a void element, that means it cannot have any child nodes inside of it including text nodes.

$('input[person]').each(function(index) {
    alert(index + ': ' + $(this).val());
});

DEMO

Comments

1

input has .val() to read values

$('input[person]').each(function(index) {
    alert(index + ': ' + $(this).val());//will read input value
  }
);

Comments

1

I have a answer for your Doubt.

You have to change the Script which you have used as below.

$('input[person]').each(function(index) {
    alert(index + ': ' + $(this).val());
  }
);

Please update me whether you find my code is useful for you.

Comments

1
   Hey you can use like this:-
   input "type=text" tags have the values not text.
   that's why you have to use .val() instead of .text().

   $('input[person]').each(function(index) {
       alert(index + ': ' + $(this).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.