I have generated multiple text boxes using PHP with name="student[<?php echo $StudentID ; ?>]"
.
Now on a button click i want to change the value of all these text boxes using jquery.
How do i do this ? Please help.
I have generated multiple text boxes using PHP with name="student[<?php echo $StudentID ; ?>]"
.
Now on a button click i want to change the value of all these text boxes using jquery.
How do i do this ? Please help.
You can use the Attribute Starts With selector, to look for student[
at the beginning of the name attribute:
$('input[name^="student["]').val('the new value');
It's probably unnecessary to include the [
at the end, and name^="student"
will be sufficient, assuming you don't have other inputs with names like student_name
or the like.
// If no conflicting named inputs, use
$('input[name^="student"]').val('the new value');
HTML
<input type="text" name="student[]"></input>
<input type="text" name="student[]"></input>
<input type="text" name="student[]"></input>
<button id="button">Change</button>
JavaScript
$('#button').click(function() {
$('input[name^="student"]').val('some value ');
});
You can also simply add a class that is unique to all of those text boxes (i.e. changableTextBox) and then select it with that and change them all at once. It's also helpful for the future if you need to adjust some styling on all of them at once. Just declare that class in CSS and you're styling.
<input type="text" class="changeableStudentTextBox" id="student[11]" />
<input type="text" class="changeableStudentTextBox" id="student[23]" />
<input type="text" class="changeableStudentTextBox" id="student[45]" />
<input type="text" class="changeableStudentTextBox" id="student[66]" />
<script type="text/javascript">
$('#button').click( function() { $('.changeableStudentTextBox').val('hi!'); });
</script>