Search code examples
javascriptformsradio-button

Checking a radio with no id and class through Javascript


I'm trying to programmatticaly check this radio buton using javascript:

<input type="radio" name="LINK_TYPE" value="normal">

But in that page there are other elements with the same name, but different values. How can I check just the one with the value="normal"?

I've tried something like this:

document.querySelector('input[name=LINK_TYPE]:checked').normal;

Many thanks!


Solution

  • Just like you used the name value selector, use the value selector.

    document.querySelector('input[name="LINK_TYPE"][value="normal"]').checked = true;
    

    Note that there should be no space between the two attribute-value selectors and input.