-1

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.

1
  • #displayname and #groupname are (or should be) elements on the page, not variables. I don't know if that's what you meant by "variables", but I thought I would clarify in case of confusion. To use variables, it should be var groupName = someValue;. Commented Feb 27, 2014 at 10:09

1 Answer 1

0

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/

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

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.