I have the following HTML:
<a href="https://datasite.com/data/?search=Datapoints+3-4%3B+Datapoints+4-5&version="
onclick="location.href=this.href + selectedVersion; return false;">Datapoints 3-4; Datapoints 4-5</a>
A <select> tag returns a string for "selectedVersion".
This is working. I need to add another OPTIONAL parameter to the url when a CheckBox is checked.
I added an <input type="checkbox" onclick="printClick()"> which returns a string for "printableVersion"
var printableVersion;
function printClick() {
var checkBox = document.getElementById("printCheckbox");
if (checkBox.checked == true){
printableVersion = "&interface=print";
} else {
printableVersion = "";
}
}
</script>
I modified the href as follows:
<a href="https://datasite.com/data/?search=Datapoints+3-4%3B+Datapoints+4-5"
onclick="location.href=this.href + printableVersion + "&version=" + selectedVersion; return false;">Datapoints 3-4; Datapoints 4-5</a>
But neither parameter are added. On this site, I can only use Javascript, not JQuery, ajax, etc.
2
Answers
Try this solution to fix :-
Update the onclick logic in your tag to use single quotes (‘) for the onclick attribute’s value, as you’re already using double quotes (") inside it.
It is generally not a good idea to use inline event handlers.
You can use event delegation to handle everything you want to handle in a document.
I would suggest you don’t use a
HTMLAnchorElement
-element. You can use a simplespan
ordiv
and use the handler to createlocation.href
from both the version selector and the print checkbox. The basic href can be stored in adata-attribute
on the clickable element.So, something like: