BillyB
BillyB

Reputation: 1

Input data undefined

I have table with input in it.

<tr id="1">
    <th>1</th>
    <th>ball</th>
    <th>10euro</th>
    <th><input type="text" class="input col-xs-1 sum" value="1"></th>
    <th style="text-align:center;"><i class="fa fa-shopping-cart fa-2x"></i></th>
    <th style="text-align:center;"><i class="fa fa-times fa-2x" id="addproduct1"></i></th>
</tr>

And script

var data = $("#1.sum").value;
alert(data);

But data is always undefined/null. The every way i try is null/undefined. Can you help me and tell what is wrong?

Upvotes: 0

Views: 187

Answers (2)

elbrant
elbrant

Reputation: 791

The error is coming from your HTML. Change your input type attribute. (Don't use the generic "text", use "number".)

Change this: <th><input type="text" ...

To this: <th><input type="number" ...

Defining the input field will eliminate "undefined" and "NaN" responses.

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

Your selector is close, but wrong. Without a space between your ID and class, you're looking for an element with an ID of 1 AND a class of sum - you want:

var data = $("#1 .sum").val();

Now it looks for an element with a class of sum that is a child of an element with the ID of 1.

Also, .value is a property on a DOM element, you have a jQuery element above, so you want .val()

Upvotes: 2

Related Questions