views:

52

answers:

3

How to redirect to a particular link if checkbox is checked using javascript?

I am doing this but its not working for me..

<input type="checkbox" name="yousendit" id="yousendit" value="1" onselect="return yousendit();"/>

<script type=javascript>
function yousendit()
{
        if(document.getElementById('yousendit').checked== "checked")
        {
            window.location='https://www.yousendit.com/dropbox?dropbox=mydomain';
            return false;
        }
        return true;

}
</script>

Please help

A: 

I don't believe onselect is a valid event for a checkbox, but I may be wrong on that.

Regardless, this works.

document.getElementById('yousendit').onclick = function() {
    if (this.checked==true)
        alert('checked'); // Or in your case, window.location = 'whatever.html';
}​​​​​​

Fiddle http://jsfiddle.net/Tm6q6/

Robert
+1  A: 

There are some problems with your source. Here is the working version:

<input type="checkbox" name="yousendit" id="yousendit" value="1" onclick="return yousendit();"/>
<script>
function yousendit(){
    if(document.getElementById('yousendit').checked){
        window.location='https://www.yousendit.com/dropbox?dropbox=mydomain';
        return false;
    }
    return true;

}
</script>

Changes:

  • onclick instead of onselect
  • checkboxes' checked property is boolean
Li0liQ
A: 

instead of using onselect use onclick event.

and instead of writing

if(document.getElementById('yousendit').checked== "checked")

write if(document.getElementById('yousendit').checked)

sushil bharwani