JavaScript Jquery javascript dropdown in a loop and calculation javascript jquery dropdown,javascript jquery dropdown append,jquery.dropdown.js,

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>
Answer:1

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

Answer:2

I have a page with 2 iframe tags: <iframe src="top.html" seamless="seamless" width=100% height=100% id="up" name="up" frameborder="0"></iframe> <iframe src="main.html" seamless="...

I have a page with 2 iframe tags: <iframe src="top.html" seamless="seamless" width=100% height=100% id="up" name="up" frameborder="0"></iframe> <iframe src="main.html" seamless="...

  1. change iframe from parent
  2. change from iframe
  3. change iframe on button click
  4. change iframe with javascript
  5. change iframe on click
  6. change iframe with button
  7. change iframe with css
  8. change iframe in js
  9. change iframe in google
  10. change iframe src from inside
  11. change iframe height from inside
  12. change iframe css from parent
  13. change iframe size from inside
  14. change iframe width from inside
  15. change iframe src from another page
  16. change iframe src from javascript
  17. change iframe src from within
  18. change iframe src from server side asp.net
  19. change iframe url from parent
  20. iframe change with jquery

I have the following lines of code in my webpage - example/demo. HTML: <p data-toggle="modal" data-target="#messagesModal"><a href="#">Messages <span class="badge">2</span>&...

I have the following lines of code in my webpage - example/demo. HTML: <p data-toggle="modal" data-target="#messagesModal"><a href="#">Messages <span class="badge">2</span>&...

I have a table in html with only 1 column: My FILES. What I'm trying to reach: (a file explorer of a folder in the server) When the page loads in body onload i call an ajax function that add a row ...

I have a table in html with only 1 column: My FILES. What I'm trying to reach: (a file explorer of a folder in the server) When the page loads in body onload i call an ajax function that add a row ...

  1. ajax request getting cancelled chrome
  2. ajax request is getting canceled before it completes
  3. ajax request not getting response

I'm trying to incorporate a React component for radio buttons in my iOS app that's written in React Native, however I get an error when trying to import the component using the method that the author ...

I'm trying to incorporate a React component for radio buttons in my iOS app that's written in React Native, however I get an error when trying to import the component using the method that the author ...

  1. error in import statement in java
  2. import statement error in eclipse