I’m working on an HTML form that will post data to my Webserver for storing into a database. I have no problem with the PHP and SQL code, but I want to have a small feature on the client side form:
There is to be a dropdown (select) that asks how many rows of a certain type you want. Then, a small JavaScript should produce these for you.
like such:
<form method="POST" action="act.php">
<select name="numrows"><option>1</option><option>2</option></select>
<table>
<tr>
<td><input type="text" name="row[x]" /></td>
</tr>
</table>
</form>
Now I would like to have as many copies of the <tr> as selected in numrows, and each with the corresponding number in the name (replacing the x). How would I implement this in JavaScript?
I understand I need to Write a method and reference this with the onchange event in the select tag, but I’m at a complete loss of how to write this function.
I’m sure you can do this in all sorts of ways with jQuery and so on, but if you’re after a portable, DOM-compliant solution, you might want to go back to basics like this:
Here I’m using the W3C DOM Level 1, and the HTML extensions. The only not-very-standard aspect is the use of the
onchangeattribute to theselectelement. If you want to be purist about this, you may wish to useaddElementListenerandattachEvent.Paste this HTML into a file and try it in Firefox and/or Internet Explorer to see whether it does the sort of thing you’re after. If you’re not convinced, do come back with questions.