I wonder if it is possible to write a Javascript that gives each link a custom format URL to go to on click.
<!-- example set one -->
<a href="javascript:myfunction()" class="relative-url" target="_blank">
<span id="input01">campfire<span>
<span id="input02">red<span>
</a>
<!-- example set two -->
<a href="javascript:myfunction()" class="relative-url" target="_blank">
<span id="input01">pepe<span>
<span id="input02">green<span>
</a>
How to write the custom javascript to formulate an URL for each <a class="relative-url">
?
<script>
// use text content of child element id=input01 and id=input02 to
// formulate an URL for it's parent div or <a>
// eg. https://www.example.com?type=campfire&color=red
// eg. https://www.example.com?type=pepe&color=green
</script>
Any good idea? Thanks a lot~
2
Answers
If you want to do it at runtime, you could do something like this. I used jQuery since you tagged it in your question.
While you’ve already accepted an answer I thought I’d offer a non-jQuery approach in case that might be of help; explanatory comments are in the code:
JS Fiddle demo.
References:
Array.prototype.forEach()
.document.querySelector()
.document.querySelectorAll()
.Element.querySelector()
.Element.querySelectorAll()
.Object.assign()
.