What would the version of this be in jquery?
document.getElementsByTagName("pre")[0].innerHTML;
I need help converting this in order to fit into my $.get
request:
$.get(
link,
function(data) {
var res = $(data).find("pre").html();
console.log(res);
}
);
What would the version of this be in jquery?
document.getElementsByTagName("pre")[0].innerHTML;
I need help converting this in order to fit into my $.get
request:
$.get(
link,
function(data) {
var res = $(data).find("pre").html();
console.log(res);
}
);
Share
Improve this question
asked Jan 15, 2014 at 0:14
JordanJordan
4952 gold badges10 silver badges22 bronze badges
2
- 2 vanilla-js. – Niet the Dark Absol Commented Jan 15, 2014 at 0:15
- What are you getting in the console log ? – adeneo Commented Jan 15, 2014 at 0:17
4 Answers
Reset to default 5The exact [JQuery equivalent] would be $('pre').eq(0).html()
. The sortof-ish mix with non-JQuery would be $('pre')[0].innerHTML
How's it work?
$('pre')
returns an Object with all elements with a tag name of pre
.eq(0)
gets the first element in the array.
DEMO
Since you're getting the first item, $('pre').first().html()
also works.
DEMO
Another thing that works would be just $('pre').html()
(Credit to RobG)
DEMO
Please note that JQuery's html
method is not identical to a browser's innerHTML
property but it's the JQuery equivalent (Credit to RobG).
Here it is in jQuery:
$('pre').first().html()
If you only need one element, give the element and ID and pull it by ID
document.getElementById("ID")
Simply specify the element name in the selector:
$("pre").html()
It's not necessary to explicitly select the first element. From the API docs:
If the selector expression matches more than one element, only the first match will have its HTML content returned