0

I am curious if pure javascript can replace this jQuery selector for IE7 and greater..

Do people simply change the markup and select using a class or is their away of selecting this using pure javascript

<h1 rel="xxxexternalxxx">Attribute Contains</h1>

var arrinput = $('[rel*="external"])

Any help would be much appreciated, thank you in advance

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Iamsamstimpson
  • 1,359
  • 2
  • 17
  • 32
  • possible duplicate of [How to get elements by attribute selector w/ native JavaScript w/o querySelectorAll](http://stackoverflow.com/questions/9496427/how-to-get-elements-by-attribute-selector-w-native-javascript-w-o-queryselector) – elclanrs Feb 20 '14 at 10:50
  • The type of selector is different to that question, this is not a basic attribute selector like the link you suggest. – Iamsamstimpson Feb 20 '14 at 10:52
  • It's not exactly a dup, but you can use that helper then filter the result by looping and comparing with regex, or substr... – elclanrs Feb 20 '14 at 10:52

1 Answers1

1

You can use getElementsByAttribute, then loop and filter:

var rels = getElementsByAttribute('rel');
var attr = 'external';

var result = [];
for (var i=0; i<rels.length; i++) {
  if (rels[i].getAttribute('rel').indexOf(attr) > -1) {
    result.push(rels[i]);
  }
}
Community
  • 1
  • 1
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • Thanks a great solution, Ill test and post the complete fiddle later today. Thanks again elclanrs – Iamsamstimpson Feb 20 '14 at 10:59
  • 1
    See my update http://jsfiddle.net/fgs2u/1/. Check the console, it it'll give you info on what's wrong, that's how I found out. – elclanrs Feb 20 '14 at 11:46