Search code examples
jqueryjquery-selectors

Jquery selector using $(this) handle next element by next()


I m trying to calculate profit percentage and write the value to next input .Using jquery , jstl

<div id="quantityHoleForPercentage">
<form:input path="quantity11Profit"  id="quantity11Profit" cssClass="smaller" />                               
</div>
<div id="quantityHoleForPercentage">
<form:input path="quantity11Price"  id="quantity11Price" cssClass="smaller" />
</div>

I m listening change event on quantity12Profit and after I got this value I m multiply it with a constant and write the result to quantity11Price

But in jquery I cannot handle $("#quantityHoleForPercentage input") in my scenerio when I changed quantity11Profit input i should be able to handle quantity11Price with $(this).next(); But I can not .I m doing that beacouse of I have too much input like these. Here is my jquery code please give some advice where I m wrong or alternative ways Thanks so much

    $("#quantityHoleForPercentage input").change(function(){                    
    var needless=$(this).attr('id');
    var willWriteToPriceInput= $(this).val();
    var priceInput=$(this).next();
    priceInput.val(willWriteToPriceInput);
    });

Solution

  • Try this:

    $("#quantityHoleForPercentage input[id$=Profit]").change(function(){
      var willWriteToPriceInput= $(this).val();
      var priceInput = $(this).parent().next()
      priceInput.val(willWriteToPriceInput);
    });
    

    But try to change quantityHoleForPercentage from ID to Class in your html and jquery