Reputation: 656
Any idea what is best practice for getting the value out of a form input field (that is second in the markup after it's label)?
I have been able to get the rest of the values fine (SKUs and price) but quantity is tricky.
JSFIDDLE to whole thing: http://jsfiddle.net/hslincoln/7HXBQ/33/
Here is some jQuery that scopes the first tr of the table and then attempts to get into that row and pull out the quantity value:
var BSKfirstQTY = $(BSKfirstrow).find('td.quantity').text('input value');
But as you can see from the fiddle's result pane, it's spewing out [object Object]
.
NOTE: Leaving the .text() empty results in the it picking up the word Qty in the fiddle result pane (which is the label, not the number).
Any help would be hugely appreciated :)
Upvotes: 1
Views: 3006
Reputation: 135
Check out Difference between val() and text() He says that .text() won't work on input elements.
.val()
.text()
I think there's not really a "best practice" scenario, since they both do different things. It just seems like it's the same, but if you're working with a form you really want to use .val()
. Otherwise .text()
might suffice.
Upvotes: 0
Reputation: 21323
If you pass an argument into text()
, you are setting the content of td.quantity
to that string, and JQuery will return the object for chaining. If you call text()
with no argument, you will get the text inside the element.
You should use .val()
to retrieve the values of input/textarea/selects elements, like so:
var BSKfirstQTY = $(BSKfirstrow).find('td.quantity input').val();
For more info on val vs. text, see this question.
Upvotes: 7