NO, this topic doesn't answer my question at all. Please read the question before doing anything.
I have a form with Javascript, which works as expected:
<script>
function checkForm(form)
{
if(form.cb1.checked) {
window.open('http://google.com/','_blank');
}
if(form.cb2.checked) {
window.open('http://yahoo.com/','_blank');
}
return true;
}
</script>
<form onsubmit="return checkForm(this);">
<label for="cb1">G</label>
<input name="cb1" type="checkbox">
<label for="cb2">Y</label>
<input name="cb2" type="checkbox">
<input type="submit" value="Submit">
</form>
But if i try to separate HTML from JS it stops working.
After clicking on Submit
an url changes to checkbox.html?cb1=on
, if the first checkbox is checked, or to checkbox.html?cb2=on
, if the second checkbox is checked, or to checkbox.html?cb1=on&cb2=on
, if checked both. But tabs with urls don't open.
My separation try looks like:
document.getElementById('cbx').addEventListener(
'submit', function checkForm(event) {
if (form.cb1.checked) {
window.open('http://google.com/', '_blank');
}
if (form.cb2.checked) {
window.open('http://yahoo.com/', '_blank');
}
return true;
});
<form id="cbx">
<label for="cb1">G</label>
<input name="cb1" type="checkbox">
<label for="cb2">Y</label>
<input name="cb2" type="checkbox">
<input type="submit" value="Submit">
</form>
<script type="text/javascript" src="form.js"></script>