Select Page
This entry has been published on 2017-02-21 and may be out of date.

Last Updated on 2017-02-21.

[:en]If you use jQuery to fetch the current value of a field like

var myvar = $('#myfield').val();

you might encounter issues, even if this syntax is correct. E.g. if a number is entered, it works. If a special char is entered, you might get an empty string.

Solution

A possible cause could be the HTML input tag attributes.

This one works not for special characters or letters:

<input type = "number" name = "myvar" id = "myvar">

Using a number type, letters stay in the field (optically), but do not seem to be saved as the value indeed, and jQuery seems to consider that.

Use special input types only if you really need them. In the example above, a value like 1234 works, but not ” 1234″ (with a space) or “=1234” or “1+2” etc.

The following should always work:

<input type = "text" name = "myvar" id = "myvar">

If you still encounter issues, you might have indeed no value in the input field. Try .html() or .text() for testing purposes.[:]