1

I am having a problem with my html document. I am trying to find the value of the iput box below but it wont work. It has that the value is blank. I am running to document as a file in my computer, not on the web, if that makes a difference. This is my html code:

<p class='textcenter'>What would you like to scan</p><input id='scanbox1' type='text' name='scanboxtype'>

And this is my jquery:

var a;
a = $('input[name=scanboxtype]').val();
alert('a is '+a)

What ever i type in, the alert pops up as 'a is'. Thanks for the help.

3
  • 'input[name="scanboxtype"]' Commented Jun 30, 2013 at 20:53
  • Is the code within an event handler, like keyup, change etc. Commented Jun 30, 2013 at 20:53
  • try $('#scanbox1').val() Commented Jun 30, 2013 at 20:53

2 Answers 2

1

You haven't triggered the alert either on submit or on change or any other events.

So you don't see the alert message with the value

Always result will be "a is"

Example:

Using .change()

$('input[name=scanboxtype]').change(function(){
    alert('a is '+ this.value); // or $(this).val()
});

Using .keyup()

$('input[name=scanboxtype1]').keyup(function(){
    alert('a is '+ this.value); // or $(this).val()
});

Refer LIVE DEMO

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

1 Comment

@user2433384: Currently I used as a direct property (that is this.value). If you want to use JQuery method .val() then use it as $(this).val()
0

Maybe your code is running when the key is pressed for example, but the value is not yet in the input element? For example on 'keydown' or something?

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.