1

Is there any difference between

 var mysel = document.getElementById("mySelect")); 
 mysel.val("1")

and

 var myobj = $("#mySelect"); 
 mysel.val("1")

In my case, I am going to change the default select option. But I can't make it work with getElementById. By rewritting it with $("#mySelect"), it works.

So I am confused about the difference between them?

Thanks!

Mariusz Jamro
  • 29,116
  • 24
  • 107
  • 151

1 Answers1

3

getElementById returns a DOM element object.

$ returns a jQuery object. Passing it a string containing an id selector causes it to populate the jQuery object with a DOM element object.

val is a jQuery method, not a DOM element method.

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264