querySelector to find label in pure JS

javascript

Solution

If you're looking for a `<label>` tag specifically, you would use:

document.querySelector('label[for="foobar"]').className = "foo";

The selector that you have will select the first element of any tag that has the given `for` attribute.

Here's a fiddle to demonstrate: http://jsfiddle.net/bryanjamesross/f53A4/

Problem

Strictly using JS, I want to select a `label` element and add a class. ``` document.querySelector('[for=foobar]').className = "foo"; ``` What should go in the `querySelector` to find `<label for="foobar">`? The error I'm getting is Uncaught SyntaxError: Failed to execute query: '[for=foobar]' is not a valid selector. Ok, I actually solved it by adding quotes around `foobar`, so it reads: ``` document.querySelector('[for="foobar"]').className = "foo"; ```

Original source