0

Is there a way in javascript to select a selector within another selector like in jQuery:

$("section h1").html();
Jason P
  • 26,984
  • 3
  • 31
  • 45
Johannes Flood
  • 735
  • 3
  • 7
  • 9

3 Answers3

3

jQuery actually uses querySelectorAll when available. Thing is, on your own, you'll never be able to emulate all the goodies that jQuery provides (e.g., filter(), find(), children(), parent(), map(), not() and the ability to use pseudo-classes).

This would be enough to grab the h1 element in section:

var e = document.querySelector("section h1");

JSFiddle.

Read on this answer https://stackoverflow.com/a/11503576/2612112.

Community
  • 1
  • 1
federico-t
  • 12,014
  • 19
  • 67
  • 111
1

Closest you'll find is probably document.querySelector or document.querySelectorAll. Should be careful though, as support is limited to modern browsers.

callmehiphop
  • 636
  • 5
  • 10
0

you also could do the following... however there are easier ways

see jsfiddle http://jsfiddle.net/kasperfish/CR5EX/2/

alert('jquery '+$("#section h1").html());

v=document.getElementById("section").getElementsByTagName("h1");

alert('javascript '+v[0].innerHTML);
kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51