I have to find and display a selector from HTML presented in array, this works for me using Jquery:
var a = '';
var b = Array.prototype.map.call($('p', a),
function(e) { return e.outerHTML; });
console.log(b)
However I don't want to use Jquery, Id rather use plain Javascript so I tried:
a.querySelectorAll('p')
Not working. Why is that and what else can I try?
3 Answers 3
You're using querySelectorAll as if it is available in the String.prototype object because the variable a is an empty string.
Try document.querySelectorAll('p');
Comments
I think what you want is this:
var b = Array.prototype.map.call(document.querySelectorAll("p"),
function(e) { return e.outerHTML; });
console.log(b);
This way your calling the query selector on the document rater than on an empty string(where the function won't exist).
Comments
Try this:
document.getElementsByTagName("p")
Which will return an array of all <p> tags
Comments
Explore related questions
See similar questions with these tags.
a. Is it a DOM element or a string? Also, what's not working? What error do you see?querySelectorAllfunction is being looked up in theArray.prototype. BecausequerySelectorAllis not found,a.querySelectorAllwill return undefined. Finally, trying to invoke an undefined value will cause that error. Try this (for example):var und = undefined;und();