Sreeraj
Sreeraj

Reputation: 2434

Javascript variable undefined error

Please have a look at this code.

$('#displayname').val(prompt('Enter your name:', ''));
$('#groupname').val(prompt('Enter your Group:', ''));
alert($('#displayname').val());
alert($('#groupname').val());

I read values for two variables displayname and groupname respectively through javascript prompts. Then when i alert these two variables displayname alerts the real value,whereas groupname is alerted as undefined. I could not find any syntactical errors. What can be the possible reason ? Thank You in advance.

Upvotes: -1

Views: 720

Answers (1)

Karthick Kumar
Karthick Kumar

Reputation: 2361

You don't have element with particular id, so it will throw an undefined error.

Please review the following code:

<input id="displayname">
<input id="groupname">

$('#displayname').val(prompt('Enter your name:', ''));
$('#groupname').val(prompt('Enter your Group:', ''));
alert($('#displayname').val());
alert($('#groupname').val());

Demo: http://jsfiddle.net/St83V/

Upvotes: 0

Related Questions