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
  • 30,615
  • 24
  • 120
  • 162

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
  • 914,110
  • 126
  • 1,211
  • 1,335