Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to prevent javascript function from changing other values in html form
    text
    copied!<p>I have two select blocks in my html form:</p> <pre><code>&lt;select id="select1"&gt; &lt;option&gt;1&lt;/option&gt; &lt;option&gt;2&lt;/option&gt; &lt;option&gt;3&lt;/option&gt; &lt;/select&gt; &lt;select id="select2" onchange="someFunc()"&gt; &lt;option&gt;A/option&gt; &lt;option&gt;B&lt;/option&gt; &lt;/select&gt; </code></pre> <p>I have a validateForm() function called from the form tag @onSubmit. the "someFunc" function populates some innerHTML elsewhere in the form. The issue I'm having, is that when I hit submit, the value of select1 reverts to its original default value, and the validateForm function fails. </p> <p>the someFunc function is really long, so I didn't post the whole thing, but essentially what it does is check a string value and populate html, like:</p> <pre><code>if(select2.value = 'A'){ document.getElementById('anotherElement').innerHTML= '&lt;h1&gt;a new tag&lt;/h1&gt;'; } </code></pre> <p>is there another att besides @onChange? </p> <p>Ok, here is a complete html form that describes the problem I am having: </p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;javascript q&lt;/title&gt; &lt;script&gt; function updateTag(){ document.getElementById('newHeading').innerHTML='&lt;p id="status"&gt;updated&lt;/p&gt;'; } function validateForm(){ var s1 = document.getElementById('select1'); var s2 = document.getElementById('select2'); if( s1.value = 'please select' ){ alert('please select a value from the pulldown'); return false; } // updated html needs to have been performed and value selected if(document.getElementById('status') == null ){ alert('status does not exist'); } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Javascript Q&lt;/h1&gt; &lt;form id="testForm" onSubmit="return validateForm();"&gt; &lt;select id="select1"&gt; &lt;option&gt;please select&lt;/option&gt; &lt;option&gt;1&lt;/option&gt; &lt;option&gt;2&lt;/option&gt; &lt;option&gt;3&lt;/option&gt; &lt;/select&gt; &lt;select id="select2" onchange="updateTag()"&gt; &lt;option&gt;please select&lt;/option&gt; &lt;option&gt;a&lt;/option&gt; &lt;option&gt;b&lt;/option&gt; &lt;option&gt;c&lt;/option&gt; &lt;/select&gt; &lt;input type="submit" value="submit"/&gt; &lt;div id="newHeading"&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>what happens is, I select a value from each pulldown, hit submit, then the value of select1 reverts to its orig val, and fails the validation</p> <p>hope that's better.</p> <p>thanks, bp</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