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

16,485

Solution 1

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

Solution 2

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/

Solution 3

use this code

<input type="checkbox" value="xyz.php"
    name="checket"
    onClick="if (this.checked) { window.location = this.value; }">
Share:
16,485

Related videos on Youtube

OM The Eternity
Author by

OM The Eternity

M a PHP Programmer.. And an Experienced Tester, And a Vocalist...

Updated on May 03, 2022

Comments

  • OM The Eternity
    OM The Eternity almost 2 years

    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