user310291
user310291

Reputation: 38180

How to get the value of input field by custom attribute in jquery

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());
  }
);

Upvotes: 0

Views: 933

Answers (5)

Aeldred
Aeldred

Reputation: 314

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

Upvotes: 2

vishu
vishu

Reputation: 231

   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());
   });

Upvotes: 1

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

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.

Upvotes: 1

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

input has .val() to read values

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

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

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

Upvotes: 1

Related Questions