I try to populate several textfields to become something like this <a href="check.php?name=john doe&group=metallica">Check</a>
Below is my script that I modified from here but this script is only show inside textarea and how to show inside anchor link and generate something like <a href="check.php?name=john doe&group=metallica">Check</a>.
<div id="textBoxContainer">
<input type="text" id="name" onkeyup="UpdateTextArea();" name="name" />
<input type="text" id="group" onkeyup="UpdateTextArea();" name="group" />
</div>
<textarea id="textAreaResult"></textarea>
<a href="check.php?" id="link_check">Check</a>
<script type="text/javascript">
function UpdateTextArea() {
var textBoxContainerDiv = document.getElementById("textBoxContainer");
var textboxes = textBoxContainerDiv.getElementsByTagName("input");
var finalResult = "";
var textAreaFinalResult = document.getElementById("textAreaResult");
for (var i = 0; i < textboxes.length; i++) {
finalResult = finalResult + textboxes[i].id + "=" + textboxes[i].value + "&";
}
textAreaFinalResult.value = finalResult;
}
</script>
If you want to target an anchor tag instead of the textarea, then obviously you need to change that accordingly:
Then in your
updateTextAreafunction, you need to target the anchor’shrefrather than the textarea’svalue. There’s several other optimizations that can be made in there also, such as making an array of name/value pairs and then doing ajoinon them so you don’t end up with the trailing&, etc.What the heck, here’s a fiddle of the whole thing.
It would probably make sense to change the name of the function itself since there’s no textarea involved anymore, but I leave that as something for you to do.