How to trigger a function when clicking a radio type input?

Asked

Viewed 951 times

2

I have 2 inputs of the type radio. In them, I’m trying to trigger my JS method. However, by clicking on radio button, nothing is done. Neither gets into the JS function:

function onView1(){
    alert("oi");
    var x = document.getElementsByName("user_1");
    if(x.value== 1){
        location.href = "option_user.html";
    }else {
        alert("nao deu");
    }
}
<tr>
    <td>
        <div align="center">    
            <br><br>
            <img src="images/Untitled-1.jpg"><br><br>
            <input type="radio" name="user_1" value="1" id="1" onkeyup="onView1()"/>
        </div>
    </td>
</tr>
  • They are 2 radio, I who did not post the rest of the code. What I want to do, is to click the radio, redirect to a specific page.

1 answer

3


If you want something to happen when you click on input, use the event onclick in place of onkeyup:

function mudaDePagina(){
  location.href = 'http://answall.com';
}
<input type='radio' onclick='mudaDePagina()'>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.