samedi 1 avril 2017

Avoiding Repetitive Javascript

I have an HTML form with about 30 similar lines in it. Each line is something like this:

<tr>
<td>Car Park</td>
<td><input type="checkbox" id="local_parking" name="amenity[]" value="local_parking"></td>
<td><input type="text" id="local_parking_t" name="amenity_text[]" value="Car park"></td>
</tr>

If the checkbox is not checked, the text box needs to be disabled to avoid it passing its value to the mySQL query that follows. I can do that for one line with this javascript:

document.getElementById('local_parking').onchange = function() {
document.getElementById('local_parking_t').disabled = !this.checked;
};

However it seems unreasonably messy to repeat the javascript 30 times over with different IDs. Is there a better way?




Aucun commentaire:

Enregistrer un commentaire