Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may want to turn ids into classes (to avoid duplication of ids) and also event delegation for the inputs that are created dynamically for the keyup event. Try this way: Since you want shapes for each of the section you can wrap them into single fieldset.</p> <h3>Html</h3> <pre><code>&lt;form&gt; &lt;fieldset class="fieldset"&gt;1 &lt;input type="text" class="test" name="test" /&gt;2 &lt;input type="text" class="test2" name="test" /&gt; &lt;div class="dimensions"&gt;&lt;/div&gt; &lt;div class="trouble"&gt;&lt;/div&gt; &lt;div class="texte"&gt;&lt;/div&gt; &lt;/fieldset&gt; &lt;input type="button" id="add_new" value="add new"&gt; &lt;/form&gt; </code></pre> <h3>Script</h3> <pre><code>$(document).ready(function () { var fieldset_parent = $(".fieldset:eq(0)").clone(); $('form').on("click", "input#add_new", function () { $("fieldset:last").after($(fieldset_parent).clone()); }); $(document).ready(function () { var fieldset_parent = $(".fieldset:eq(0)").clone(); $('form').on("click", "input#add_new", function () { $("fieldset:last").after($(fieldset_parent).clone()); }); $('form').on('keyup', ".test, .test2", function () { var $parent = $(this).closest('.fieldset'); //Get the closest fieldset (parent) of the textbox under question. var width = parseInt($(".test", $parent).val()); //get the value of textbox within the parent, as the context. var height = parseInt($(".test2", $parent).val());//get the value of textbox within the parent, as the context. var max = 200; var min = 20; var ratio; $(".trouble", $parent).html(function(){ //Set the html based on width &gt;=height return width &gt;= height ? "Width bigger" : "height bigger"; }); ratio = max / width; width = ratio * width; height = height * ratio; $(".dimensions", $parent).html(width + " x " + height); $(".texte", $parent).css({ "width": width + "px", "height": height + "px" }); }); }); </code></pre> <p><strong><a href="http://jsfiddle.net/Cy2Wt/" rel="nofollow">Demo</a></strong></p> <ul> <li><strong><a href="http://api.jquery.com/closest/" rel="nofollow">.closest()</a></strong></li> <li><strong><a href="http://api.jquery.com/html/#html-functionindex--oldhtml" rel="nofollow">.html(func)</a></strong></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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