Skip to content Skip to sidebar Skip to footer

Why .val() Is Always Empty (jquery)?

I have a problem with an Input. here's my html/php code : $dotation = ($ig['dotation']) ? $ig['dotation']: '

Solution 1:

.val() grabs the value attribute of a input-element. ".dot" is a td and doesn't have a value-attribute.

What exactly is it you want dot_cha to be? If its the input you are after, this would work.

$(this).parent().find('.datepickerIG').val();

If it is the text of the td .html() or .text() will give you this.

Solution 2:

val retrieves the value of input and other tags which have this attribute. In your case, .dot is the class of a table row which has no attribute value... So you have to use .text() or .html().

Solution 3:

I would say that the problem is a <td> doesnt have a value, it contains HTML.

That would be why .html works and .val doesnt.

You use .val on inputs that have values, but <td> is not an input its just a HTML tag.

Solution 4:

val() is to be used with form elements (such as input, textarea etc...), not with elements.

You need to get the innerHTML or innerText of the element.

Something like:

var dot_cha = $(this).parent().find('.dot').text();

Though you need to ensure you have one element - as you may be getting a collection of elements.

Solution 5:

From the docs:

The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of elements, the .val() method returns an array containing each selected option; if no option is selected, it returns null.

Post a Comment for "Why .val() Is Always Empty (jquery)?"