Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to preform operation in javascript
    primarykey
    data
    text
    <p>I want to subtract 2 values, and do the calculation instantly with javascript. I need to find a way to target second <code>&lt;select&gt;</code> id in JS, since i echo the options, I do it like this:</p> <pre><code>&lt;tr&gt; &lt;!-- first row, with css --&gt; &lt;td style="width:50%;"&gt;From&lt;/td&gt; &lt;td style="width:50%;"&gt; &lt;select name="fromlevel" id="fromlevel" style="width:100%; text-align:center; font-weight:bold;"&gt; &lt;?php $i = 1; while ($i &lt; 91) { echo ' &lt;option value=f' . $i . ' name=f' . $i . '&gt;' . $i . '&lt;/option&gt;'; $i++; } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;!-- second row, with css --&gt; &lt;td&gt;To&lt;/td&gt; &lt;td&gt; &lt;select name="tolevel" id="tolevel" style="width:100%; text-align:center; font-weight:bold;"&gt; &lt;?php $i = 1; while ($i &lt; 91) { echo ' &lt;option value=t' . $i . ' name=t' . $i . '&gt;' . $i . '&lt;/option&gt;'; $i++; } ?&gt; &lt;/select&gt; &lt;/td&gt; &lt;/tr&gt; </code></pre> <p>I referenced the IDs with f1, f2, f3, f4 etc, and t1, t2, t3, t4 etc. Hhow do i distinguish between them in JS?</p> <p>The JS below works if I just refernce the ID of the first <code>&lt;select&gt;</code> as $i, im very bad with JS, and i dont know how to make that reference f$i </p> <pre><code>var level_current_prices = {}; for(var i = 1; i &lt;= 90; i++ ) { level_current_prices[i] = i; } function getCurrentLevel() { // current level, from var levelSetPrice=0; var theForm = document.forms["priceCalcForm"]; var selectedLevel = theForm.elements["fromlevel"]; levelSetPrice = level_current_prices[selectedLevel.value]; return levelSetPrice; } function calculateTotal() { var LevelPrice = getCurrentLevel(); var divobj = document.getElementById('totalPrice'); divobj.style.display='block'; divobj.innerHTML = "Total Price For the Leveling $"+LevelPrice; } function hideTotal() { var divobj = document.getElementById('totalPrice'); divobj.style.display='none'; } document.forms['priceCalcForm'].fromlevel.onchange = function () { calculateTotal(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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