javascript - jQuery equivalent of document.getElementById - Stack Overflow

I have a piece of JavaScript code that I want to translate it to jQuery:var target = document.getElemen

I have a piece of JavaScript code that I want to translate it to jQuery:

 var target = document.getElementById('myID');

 ... do additional work here ...

The code above works just fine, no problem. However if I try to use it this way, nothing happens:

 var target =  $("#myID");

What is the equivalent of document.getElementById in jQuery?

P.S. jQuery library is loaded and the code is withing

    $(document).ready(function () {


       var target = document.getElementById('myID');

       ... do additional work here ...


       var target =  $("#myID");

    });

I have a piece of JavaScript code that I want to translate it to jQuery:

 var target = document.getElementById('myID');

 ... do additional work here ...

The code above works just fine, no problem. However if I try to use it this way, nothing happens:

 var target =  $("#myID");

What is the equivalent of document.getElementById in jQuery?

P.S. jQuery library is loaded and the code is withing

    $(document).ready(function () {


       var target = document.getElementById('myID');

       ... do additional work here ...


       var target =  $("#myID");

    });
Share Improve this question edited Jul 23, 2014 at 7:08 David Dury asked Jul 23, 2014 at 6:52 David DuryDavid Dury 5,72712 gold badges60 silver badges97 bronze badges 10
  • 1 any error in console? – Mritunjay Commented Jul 23, 2014 at 6:52
  • 3 Included Jquery library..? getting any errors like $ is not defined displaying in the console.? – Rajaprabhu Aravindasamy Commented Jul 23, 2014 at 6:52
  • 1 "do additional work here" what is the additional work there? – Anoop Joshi P Commented Jul 23, 2014 at 6:53
  • 1 This is the most basic thing you can do in jQuery. Have you tried debugging in any way? Included jquery.js? Checked the console for errors? Does the element actually exist on DOM load? Included a DOM Ready handler? – Rory McCrossan Commented Jul 23, 2014 at 6:53
  • 2 Thanks @undefined it works! Didn't knew about the get(0) ... :) great! – David Dury Commented Jul 23, 2014 at 6:56
 |  Show 5 more ments

3 Answers 3

Reset to default 3

As @undefined pointed in a ment above, the correct answer is:

var target = $("#myID").get(0);

how about var target = $("#myID")[0];

recently i debug the code :

var map = new google.maps.Map(document.getElementById("myId"), mapOptions);

it works well only with getElementById , and the $("#myID") not work.

try this code it will help you..

var target = $("#myID")[0];

thanks

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744967440a4603766.html

相关推荐

  • javascript - jQuery equivalent of document.getElementById - Stack Overflow

    I have a piece of JavaScript code that I want to translate it to jQuery:var target = document.getElemen

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信