John Davidson

javascript - Show Hide Input element on Reapeater based on selection

0 comments
Message:


I want to create show input element using jquery after selected dropdown and show the input element in repeater but the problem I only show the input element in one input element, Thanks for your help.


This is html code form repeater


 <tbody>
@foreach($siswas as $key => $nilai)
<tr>
<td><input type="text" class="form-control" id="nilai_sikap" name="nilai_sikap[]"></td>
</tr>
@endforeach
</tbody>

This is script to show and hide input element in id="nilai_sikap"


    $('#mapel_id').on('change', function() {
if ( this.value == 17 || this.value == 18)
$("#nilai_sikap")[$i].show();
else
$("#nilai_sikap").hide();
}).trigger("change")

From this I only get one input element like this
enter image description here


but I want show input element in every row like this
enter image description here



My Answer:
To achieve this, you can use the `closest()` and `find()` methods in jQuery to target the specific input element within the same row as the dropdown selection. Here's an updated version of your script:

javascript
$('#mapel_id').on('change', function() {
var $selectedInput = $(this).closest('tr').find('#nilai_sikap');

if (this.value == 17 || this.value == 18) {
$selectedInput.show();
} else {
$selectedInput.hide();
}
}).trigger("change");


With this script, when the dropdown selection changes, it will find the closest `tr` element and then find the input element with the id `nilai_sikap` within that row. It will then show or hide that specific input element based on the dropdown selection.

This way, you can show or hide the input element in every row of the repeater based on the dropdown selection.

Rate this post

4 of 5 based on 6099 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us