What is the best way to re-use composed html elements, in particular forms?
Say I have a basic voting form similar to this (in a separate file voteform.html
):
<form>
<div>
<input id="bad" type="radio" name="vote" value="-1"/>
<label for="bad" style="background: #b66; width:100px; height:100px; display:inline-block"></label>
<input id="good" type="radio" name="vote" value="+1"/>
<label for="good" style="background: #6b6; width:100px; height:100px; display:inline-block"></label>
</div>
</form>
I now want to let users of my website vote on several questions using the same form. What is the best / cleanest / most elegant way to re-use the above code snippet throughout my website (the questions asked may change dynamically over time, but the answer options in the form would always be the same)?
If i simply .clone()
the form or load the content via
var x = $('<div/>').load('./voteform.html');
votes_div.append(x)
the radio buttons do not behave as expected because the ids are the same for each clone and all changes will only affect the first form.
This question explains how to change the id when cloning elements, is this the best option i have? Seems like a bit of a workaround.