0

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?

asked Sep 24, 2014 at 17:20
5
  • 3
    What's a. Is it a DOM element or a string? Also, what's not working? What error do you see? Commented Sep 24, 2014 at 17:25
  • Works just fine -> jsfiddle.net/knh2n8wy Commented Sep 24, 2014 at 17:29
  • I was doing something wrong...hard to explain: listening to event DOM mutations and try to alter them but now I realize I was just calling from the DOM...anyway Im going to accept an answer as it fits the question. a= array from mutation record and console says 'undefined is not a function' Commented Sep 24, 2014 at 17:31
  • 1
    You might be getting the 'undefined is not a function' error because the querySelectorAll function is being looked up in the Array.prototype. Because querySelectorAll is not found, a.querySelectorAll will return undefined. Finally, trying to invoke an undefined value will cause that error. Try this (for example): var und = undefined; und(); Commented Sep 24, 2014 at 17:39
  • @Joseph Ahn I know, but I thought I was doing something else. But when I saw that document.queryselector worked I realized I was just calling things from the document instead from within the scope of my mutation function (arrays/strings and what else) Commented Sep 24, 2014 at 17:43

3 Answers 3

2

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');

answered Sep 24, 2014 at 17:26
Sign up to request clarification or add additional context in comments.

Comments

1

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).

answered Sep 24, 2014 at 17:27

Comments

1

Try this:

document.getElementsByTagName("p")

Which will return an array of all <p> tags

answered Sep 24, 2014 at 17:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.