Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery - Get the value of closest textbox and assign it into a variable
    text
    copied!<p>How can I get the value of the closest textbox and assign it into a variable? Then after getting the value, change the current value of that textbox by multiplying it by the value of previous textbox. I have three instances of divs with the same input elements so I think .closest() or .next() can help me. Here is a snippet of my code</p> <p>HTML</p> <pre><code>&lt;div class="box"&gt; &lt;b&gt;1&lt;/b&gt; &lt;input type="text" name="quantity" value="1"/&gt; &lt;input type="checkbox" name="ignoreThisCheckbox" checked="checked"/&gt; &lt;input type="text" name="price" value="10"/&gt; &lt;/div&gt; &lt;div class="box"&gt; &lt;b&gt;2&lt;/b&gt; &lt;input type="text" name="quantity" value="1"/&gt; &lt;input type="checkbox" name="ignoreThisCheckbox" checked="checked"/&gt; &lt;input type="text" name="price" value="10"/&gt; &lt;/div&gt; &lt;div class="box"&gt; &lt;b&gt;3&lt;/b&gt; &lt;input type="text" name="quantity" value="1"/&gt; &lt;input type="checkbox" name="ignoreThisCheckbox" checked="checked"/&gt; &lt;input type="text" name="price" value="10"/&gt; &lt;/div&gt; </code></pre> <p>JS</p> <pre><code>$("input[name=quantity]").each(function(){ $(this).bind("change keyup", function(){ var q = $(this).val(); var p = parseFloat($(this).closest("input[name=price]").val()).toFixed(2); var amount = q * p; $(this).closest("input[name=price]").val(amount) console.log(amount); }) }) </code></pre> <p>non working demo</p> <p><a href="http://jsfiddle.net/c6yfS/" rel="nofollow">http://jsfiddle.net/c6yfS/</a></p> <p>Thank you for responses</p> <p><strong>/* NOTE */</strong></p> <p>The purpose of the checkbox is just to show that I have a checkbox in between the two elements concerned.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload