submit link with javascript when selecting checkbox

0

I have a checkbox, and I need the javascript to send the link according to the selected checkbox.

Example

html:

<input type='checkbox' id='1'>
<input type='checkbox' id='2'>
<input type='checkbox' id='3'>

Javascript

Well javascrip has to call the following link

http://link do site/index.php?id=(id igual ao do checkbox) 

Does anyone know how to do this with javascript?

    
asked by anonymous 05.03.2016 / 14:31

1 answer

1

You can capture the value of the selected element by navigating through the DOM tree.

var elementA = document.getElementById("a");
var elementB = document.getElementById("b");
var elementC = document.getElementById("c");

var urlComplement = "id=";

elementA.addEventListener("click", display);
elementB.addEventListener("click", display);
elementC.addEventListener("click", display);

function display(){
    if(this.checked){
	alert(urlComplement + this.value);
    }
}
<input type="checkbox" id="a" value=1>
<input type="checkbox" id="b" value=2>
<input type="checkbox" id="c" value=3>

There are several better alternatives than the one posted in the example, I strongly recommend reading link , also look for manipulation DOM tree via Javascript will assist highly in the projects.

    
05.03.2016 / 18:49