Reputation: 495
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);
}
);
Upvotes: 0
Views: 11763
Reputation: 13809
The exact [JQuery equivalent] would be $('pre').eq(0).html()
. The sortof-ish mix with non-JQuery would be $('pre')[0].innerHTML
$('pre')
returns an Object with all elements with a tag name of pre
.eq(0)
gets the first element in the array.
Since you're getting the first item, $('pre').first().html()
also works.
Another thing that works would be just $('pre').html()
(Credit to RobG)
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).
Upvotes: 5
Reputation: 60414
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
Upvotes: 0
Reputation:
If you only need one element, give the element and ID and pull it by ID
document.getElementById("ID")
Upvotes: 0