2

I got a a dropdown list populating from a php while loop. There will be more than one select dropdown boxes. When i select any dropdown from the loop, the subsequent textbox should change the value.

<html>
   <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title></title>
      <script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>
      <script type='text/javascript'>//<![CDATA[
         $(window).load(function(){
         $("#category").change(function () {
         var dept_number = $(this).val();
         var price = $(this).find(':selected').data('price');
         $('#dept-input').val(dept_number);
         $('#price-input').val(price);
         });
         });//]]> 


      </script>
   </head>
   <body>
      <select id="category[]">
         <option value="1" data-price="10">Item 1 second</option>
         <option value="2" data-price="20">Item 2 second</option>
         <option value="3" data-price="30">Item 3 second</option>
      </select>
      <br><br>
      <label>Dept. num:</label>
      <input type="text" id="dept-input[]"></input>
      <br><br>
      <label>Price:</label>
      <input type="text" id="price-input[]"></input>
   </body>
</html>

This script is working with out the loop. How will i change it to work with the loop.. Thank you in advance

<script type='text/javascript' src='http://code.jquery.com/jquery-1.10.1.js'></script>

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function(){
    $("select").on('change',function () {
        var dept_number = $(this).val();
        var price = $(this).find(':selected').data('price');
        $(this).siblings('.deptip').val(dept_number);
        $(this).siblings('.priceip').val(price);
        $(this).siblings('.total1').val(price * $(this).siblings('.total').data('value'));
    });
});
});//]]> 

</script>
<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
    <td><select id="category0">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select></td>
    <td><label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
    <td><label>Price:</label>
    <input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
    <td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
	<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>

</tr></table>
</div>

<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
    <td><select id="category1">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select></td>
    <td><label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
    <td><label>Price:</label>
    <input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
    <td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
	<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>

</tr></table>
</div>

<div class="base"> <!--Keep each group in base container-->
<table border="1"><tr>
    <td><select id="category2">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select></td>
    <td><label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--give them a class--></td>
    <td><label>Price:</label>
    <input type="text" class="priceip" id="price-input"/> <!--a class here too--></td>
    <td><label>Total:</label><input data-value="50" type="text" readonly class="total"/></td>
	<td><label>Total1:</label><input data-value="50" type="text" readonly class="total1"/></td>

</tr></table>
</div>

0

2 Answers 2

4

DEMO

  • Categorize your multiple selects into some parent div container and assign class to input elements in each container

For ex:

<div class="base"> <!--Keep each group in base container-->
    <select id="category">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select>
    <label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--give them a class-->
    <label>Price:</label>
    <input type="text" class="priceip" id="price-input"/> <!--a class here too-->
</div>

<div class="base">
    <select id="category1">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select>
    <label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/><!--same goes here-->
    <label>Price:</label>
    <input type="text" class="priceip" id="price-input"/><!--and here too-->
</div>

<div class="base">
    <select id="category2">
       <option value="1" data-price="10">Item 1 second</option>
       <option value="2" data-price="20">Item 2 second</option>
       <option value="3" data-price="30">Item 3 second</option>
    </select>
    <label>Dept. num:</label>
    <input type="text" class="deptip" id="dept-input"/>
    <label>Price:</label>
    <input type="text" class="priceip" id="price-input"/>
</div>
  • Write a common jquery on change event to select element.

For Ex:

$(document).ready(function(){ //Write it in document.ready()
    $("select").on('change',function () {
        var dept_number = $(this).val();
        var price = $(this).find(':selected').data('price');
        $(this).siblings('.deptip').val(dept_number); //find its siblings with classname 
        $(this).siblings('.priceip').val(price);
    });
});

Update

DEMO

Hoping the html will be there for each group

<label>Total:</label><input data-value="50" type="text" readonly class="total"/><br/>
<!-- Added value fetched from php to data-value attribute of input-->

So your modified JS would be as below:

$(document).ready(function(){
    $("select").on('change',function () {
        var dept_number = $(this).val();
        var price = $(this).find(':selected').data('price');
        $(this).siblings('.deptip').val(dept_number);
        $(this).siblings('.priceip').val(price);
        $(this).siblings('.total').val(price * $(this).siblings('.total').data('value'));//Add this line
    });
});

UPDATE 2

DEMO

For your current structure you could change your script as below:

$(document).ready(function(){
    $("select").on('change',function () {
        var dept_number = $(this).val();
        var price = $(this).find(':selected').data('price');
        $(this).closest('table').find('.deptip').val(dept_number);
        $(this).closest('table').find('.priceip').val(price);
        $(this).closest('table').find('.total').val(price * $(this).closest('table').find('.total').data('value'));
    });
});

Basically here we are finding the parent table of the clicked select and then finding required elements within that parent

Sign up to request clarification or add additional context in comments.

22 Comments

It is working fantastic. Thank you. I have one more doubt. I have a field called Total in all the loop with the select. Its a readonly field with a value. Now when i select from a dropdown, it should calculate price*total and show it another field. Its actually a continuation for me. Can u pls help me Guru with this also?
Or do i need to post as a separate question?
Total for each select group?
iam really sorry for this. If any more doubts, i will make a different question. Really appreciate the help. Thanx
Yes it helped me a lot. Thank you so much. :)
|
1

I think the only problem in your code is that the resulting HTML contains [] square brackets inside of the id attribute of some elements. Removing them should fix your problem: Example.

Otherwise, if you cannot modify HTML then, escape the characters when trying to access them in JavaScript: Example.

And for multiple select elements, you can do this.

Snippet:

var categories = $('select[id^=category]');
var deptNumbers = $('input[id^=dept-input]');
var priceNumbers = $('input[id^=price-input]');
categories.each(function(index) {
    $(this).change(function() {
        var dept_number = $(this).val();
        var price = $(this).find(':selected').data('price');
        deptNumbers.eq(index).val(dept_number);
        priceNumbers.eq(index).val(price);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<select id="category[0]">
    <option value="1" data-price="10">Item 1 second</option>
    <option value="2" data-price="20">Item 2 second</option>
    <option value="3" data-price="30">Item 3 second</option>
</select>
<select id="category[1]">
    <option value="1" data-price="10">Item 1 second</option>
    <option value="2" data-price="20">Item 2 second</option>
    <option value="3" data-price="30">Item 3 second</option>
</select>
<br>
<br>
<label>Dept. num:</label>
<input type="text" id="dept-input[0]"></input>
<br>
<br>
<label>Price:</label>
<input type="text" id="price-input[0]"></input>
<br>
<br>
<label>Dept. num:</label>
<input type="text" id="dept-input[1]"></input>
<br>
<br>
<label>Price:</label>
<input type="text" id="price-input[1]"></input>

Hope this helps.

6 Comments

The script is working actually. Iam trying to change the script to work in a loop. Because the select boxes are coming inside a loop. There will be more than one select box and subsequent textboxes.
@SujithNair: modified the same snippet just now. I initially thought you would have multiple select elements but the same dept-input and price-input fields, but now I have modified to cater multiple fields as well.
Though I recommend getting rid of the [] square brackets in IDs: Link and Link.
Its working Tahir. Thankx a lot. It is working fantastic. Thank you. I have one more doubt. I have a field called Total in all the loop with the select. Its a readonly field with a value. Now when i select from a dropdown, it should calculate price*total and show it another field. Its actually a continuation for me. Can u pls help me with this also?
Or do i need to post as a separate question?
|

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.