1

I make input form using static row in 7 rows. How can I get selected value in each textbox when drop down list selected? I tried to use jQuery to do it, but my code is getting error (all textbox changed). So, below is my jQuery code:

<script>
  $(document).ready(function() {
    $('[name="nm_pot_part_shortage[]"]').on('change', function() {
      $('[name="nm_maker[]"]').val($(this).val());
    });
  });
</script>

Image of my form:

each textbox depend on drop down list

1
  • 1
    do you have a jfiddle of this? Commented Mar 16, 2016 at 2:04

2 Answers 2

1

You must escape bracket notation by using double slashes for selector having name like $('[name="nm_pot_part_shortage\\[\\]"]'), otherwise you will get an error, see following code :

$( '[name=nm_pot_part_shortage\\[\\]]' ).on( 'change', function () {
   // get current value
   var selectVal = $( this ).val();
   // find the input by traversing up the parent first using .closest()
   // and use .find() to find particular input exist on the same rows with
   // select element, so this never go down to match another input exist on 
   // another rows
   $( this ).closest( 'tr' ).find( '[name=nm_maker\\[\\]]' ).val( selectVal );
});

DEMO(example given were using table)

Better case is just give all the select and input with the same class name for each element. As example give class name myselect to select element and myInput for input element, at the end JS code would be :

$('.mySelect').on('change', function () {
  var selectVal = $( this ).val();
  $( this ).closest( 'tr' ).find( '.myInput').val(selectVal);
});
Sign up to request clarification or add additional context in comments.

27 Comments

I've tried your code, voilaaa it's work. Thank you for your great answer!
No worries, glad to hear that and you're welcome mate
But, i can't get value of nm_maker in database, here's my ddl code: <select name="nm_pot_part_shortage[]" style="width:100px;" onChange="return data(this.value)"> <option value="0">--PILIH-- <?php $query=mysql_query("SELECT * FROM mst_pot_part_shortage"); while ($nm_pot_part_shortage=mysql_fetch_array($query)) { ?> <option value="<?php echo $nm_pot_part_shortage['nm_maker']?>"><?php echo $nm_pot_part_shortage['nm_pot_part_shortage']?> <?php } ?> </select>
1) So, all the values listed already on select element ? 2) Is it all the rows dynamically created? 3) If you used above JS code no need for onChange="return data(this.value)" this code
1) yes, all values listed in select statement 2) rows static, limit to 7 rows only
|
0

Native Javascript:

1) Give each dropdown an ID. This can be done in the HTML or dynamically in javascript.

2) Assign a function to each dropdown for an onclick event.

3) Retrieve the value as such:

function dropdownClicked(){
  var ID = this.id;
  var element = document.getElementById(ID);
  var value = element.value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.