tags:

views:

42

answers:

3

Hi,

I use qQuery as a glue to obtain/search original DOM elements by ID to use their original properies and methods. jQuery provides the function get() for this. How does this work with the input element?

<html>
<head>
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
<script>
$(function () {
    var textField = $('#inputId').get();
    //var textField = document.getElementById("inputId");
    alert("textField = " + textField +
        " name = " + textField.name +
        " value = " + textField.value);
});
</script>
</head>
<body>
<form>
<input id="inputId" value="3" name="region"/>
</form>
</body>
</html>

The example works with document.getElementById(), but it fails with jQuery. Why? How can I get with jQuery the same object as with document.getElementById()?

+3  A: 

Either use get(0) or just [0]:

var textField = $('#inputId').get(0);

or

var textField = $('#inputId')[0];

get() without a parameter returns an array of elements, even if there is just one. So you need to specify a zero based index.

Doug Neiner
A: 

you might want to try and use:

$('#inputId').get(0)

specifying that you want the element at index 0 of the returned array of elements. Most likely the get() function is returning an array containing one element, but it's still an array.

John Boker
A: 

Try $('#inputId').get(0);

Teja Kantamneni