views:

64

answers:

2

Hello everybody,

this is my input field:

$("<td class='testclass'><input id='field-search' value='' type='text'></td>")

How do I get the value of the input field?

I tried it this way:

var mytest =$('#field-search').val();
alert(mytest);

The result is that I get an alert being undefined though the input field has got a value e.g. 10.

Is there a way to get the value when the value of the input field changes?

A link or an example would be very helpful. Thank you.

Link to the code: http://www.file-upload.net/download-2902420/test.user.js.html

A: 

In jQuery

var myVal = $('#field-search').val();

In vanilla JavaScript

var myVal = document.getElementById('field-search').value;
scunliffe
I can't see any difference to my code to be honest.
sincerely
+1  A: 

That's because you've not appended that html to the page. Either do that (someElement.append($("<td>...</td>"));), or search in that part specifically

var e = $("<td class='testclass'><input id='field-search' value='1' type='text'></td>");
var mytest =e.find('#field-search').val();
alert(mytest);

You don't insert element into the page just by calling $("...").

Nikita Rybak
I updated my answer: http://www.file-upload.net/download-2902420/test.user.js.html
sincerely
@sincerely Could you post it on less ad-infested website with English interface and in plain text (without a need to download file)? E.g., jsfiddle.net is popular here.
Nikita Rybak
I'm sorry, seems like I lost my question already. Sorry for asking.
sincerely