Alexica
Alexica

Reputation: 13

Syntax error - how to get value of textbox

I have a trouble getting value from text box. Conventional javascript getElementByName returns error :undefined. I was able to use jQuery in other examples with select. But can't find a reference anywhere about input tags. This line shows syntax error:

  var total = = $('input[name='+formQty+'] :text').val();

What is a jquery for picking value/text of a textbox?

Upvotes: 0

Views: 1300

Answers (3)

nnnnnn
nnnnnn

Reputation: 150020

Regarding your problem getting this to work with getElementByName() - that's probably because the method is actually getElementsByName() - note the plural elementS. It returns a list of all elements with the specified name, accessible via array syntax; even if there is only one matching element you still get a list (with only one thing in it).

If you are sure there will be exactly one element with the specified name you can do this:

var elementValue = document.getElementsByName(name)[0].value;

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190905

you have 2 equal signs.

var total = $('input[name='+formQty+'] :text').val();

It might be easier also to stick an ID attribute on the input tag and use this

var total = $('#myInputId').val();

make sure you are calling it from a loaded dom:

$(function() {
   var total = $('#myInputId').val();
});

Upvotes: 2

mistagrooves
mistagrooves

Reputation: 2347

From what I understand your query selector is incorrect.

It should be 'input[name='+formQty+']:text' the :text as you have it is trying to select any text elements underneath the current input resulting in nothing. You can even get rid of the :text and only select the element based on name. .val() should then work if you have properly selected the input element.

You should also debug this by just performing the $('...') method without the function call and see what elements are returned.

Upvotes: 1

Related Questions