Hi all, just a quick question. I believed that $('#value') was a jQuery function equivalent to document.getElementById('value') however, in the code below, only the second function gives the desired result.

Can someone explain why this is?

        $('#value').value = "1";
        document.getElementById("value").value = "1";

Recommended Answers

With jQuery it should be this:

$('#value').val("1")

http://api.jquery.com/val/

Jump to Post

All 3 Replies

Ah, cheers!

Just so you know:

$('#value') // jquery object
//set value as
$('#value').val(1);

$('#value')[0] // DOM object
// so you can also set the value as
$('#value')[0].value = "1"; // This is not recommended, it's just to show that jQuery objects relay on the DOM object itself
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.