0

Lets say i have this HTML code:

<a href="mysite.com" rel="cookie"></a>
<a href="mysite.com" rel="cake"></a>
<a href="mysite.com" rel="cookie"></a>

Is there a quick way to select only the 2 a tags with the cookie rel value.

I did manage to do it using for loop and checking the value on each element, but is there a shorter way?

3 Answers 3

2

You could try

document.querySelectorAll("a[rel=cookie]");
Sign up to request clarification or add additional context in comments.

Comments

1

If you don't mind using a JavaScript library, you can use jQuery

 var cookies = $('a[rel="cookie"]')

more info on jQuery selectors: http://api.jquery.com/category/selectors/

This has the added benefit of working with older browsers.

Comments

0

For modern browsers use querySelectorAll

like

var els;
if(document.querySelectorAll){
    els = document.querySelectorAll('a[rel="cake"]')
} else {
    //use a loop
}

Comments

Your Answer

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.