Getting DOM element value using pure JavaScript

Update: The question was edited. Both of the solutions are now equivalent.

Original answer

Yes, most notably! I don’t think the second one will work (and if it does, not very portably). The first one should be OK.

// HTML:
<input id="theId" value="test" onclick="doSomething(this)" />

// JavaScript:
function(elem){
    var value = elem.value;
    var id    = elem.id;
    ...
}

This should also work.

Leave a Comment