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
3 Answers
Reset to default 3As @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
评论列表(0条)