I have some checkboxes, and i want to have a chance to check/uncheck them with onclick="". For example:
<input type="checkbox" onclick="onOff();document.location.href='index.php?goto=somewhere';return false;" class="filter">
So if it is already checked, it should be unchecked and vice versa. What can be inside onOff() ? The goal is to make redirect with "goto" only if checkbox was checked. And is it was unchecked - redirect to "index.php" without "goto"
CodePudding user response:
So if it is already checked, it should be unchecked and vice versa.
This already happens. It's just that your return false;
in your onclick
inline JS means that the default action (i.e. to uncheck - or check - the checkbox) will not be taken.
Just remove the return false;
part. You don't need to do anything else.
CodePudding user response:
something like that ?
const
chkBxDirection = document.querySelector('#chkBx-direction')
, linkDirection = chkBxDirection.nextElementSibling
, directions =
[ { href:'index.php?goto=somewhereOne', txt:'somewhere One direction' }
, { href:'index.php?goto=somewheretwo', txt:'somewhere Two direction' }
];
chkBxDirection.checked = false // according to linkDirection init
chkBxDirection.onclick =_=>
{
let index = Number(chkBxDirection.checked)
linkDirection.href = directions[index].href
linkDirection.textContent = directions[index].txt
}
<input type="checkbox" id="chkBx-direction">
<a href="index.php?goto=somewhereOne">somewhere One direction</a>