Note that there are some explanatory texts on larger screens.

plurals
  1. POchange input size depending on option selected
    primarykey
    data
    text
    <p>I've seen similar questions posted and tried to change them to meet my needs but I don't know enough about javascript to do it. All I want to do is limit a form input to 2 characters if they select "state" as their search option. This is what I have:</p> <pre><code>function changeValue(){ var option=document.getElementById('searchtype').id; if (option == ("A") || ("B") ){ document.getElementById('field').size="40"; } else if(option=="C"){ document.getElementById('field').size="2"; &lt;form action="results.php" method="post"&gt; Search By:&lt;br /&gt; &lt;select id="searchtype" name="searchtype" onchange="changeValue();"&gt; &lt;option id="A" value="name"&gt;Hospital Name&lt;br /&gt;&lt;/option&gt; &lt;option id="B" value="city"&gt;City&lt;/option&gt; &lt;option id="C" value="state"&gt;State&lt;/option&gt; &lt;/select&gt; &lt;br /&gt;&lt;br /&gt; Search:&lt;br /&gt; &lt;input id="field" name="searchterm" type="text" size="0"&gt; </code></pre> <p>Am I doing something wrong or is there a better way to do this?</p> <p>I used Jack's code below and added a field.size attribute so my input matched the max allowed characters: (thanks Jack)</p> <pre><code>script type="text/javascript"&gt; function changeValue(dropdown) { var option = dropdown.options[dropdown.selectedIndex].value, field = document.getElementById('field'); if (option == 'name' || option == 'city') { field.maxLength = 40; field.size = 40; } else if (option == 'state') { field.value = field.value.substr(0, 2); field.maxLength = 2; field.size = 2; } } &lt;/script&gt; &lt;h1&gt;Hospital Search&lt;/h1&gt; &lt;form action="results.php" method="post"&gt; Search By:&lt;br /&gt; &lt;select id="searchtype" name="searchtype" onchange="changeValue(this);"&gt; &lt;option id="name" value="name"&gt;Hospital Name&lt;br /&gt;&lt;/option&gt; &lt;option id="city" value="city"&gt;City&lt;/option&gt; &lt;option id="state" value="state"&gt;State&lt;/option&gt; &lt;/select&gt;&lt;br /&gt; Search:&lt;br /&gt; &lt;input id="field" name="field" type="text" size="40"&gt; &lt;/input&gt; </code></pre> <p>After deciding a dropdown with a list of states would be better, I changed it to this:</p> <pre><code>&lt;form action="results.php" method="post"&gt; Hospital Name:&lt;br /&gt; &lt;input name="searchterm_name" type="text" size="40"&gt; &lt;br /&gt; &lt;input type="submit" name="submit" value="Search"&gt; &lt;/form&gt;&lt;br /&gt; &lt;form action="results.php" method="get"&gt; City Name:&lt;br /&gt; &lt;input name="searchterm_city" type="text" size="40"&gt;&lt;br /&gt; &lt;select name="select_state"&gt; &lt;option value=""&gt;None &lt;option value="AL" Selected&gt;Alabama &lt;option value="AK"&gt;Alaska &lt;option value="AZ"&gt;Arizona &lt;/SELECT&gt; &lt;input type="submit" name="submit" value="Search"&gt; &lt;/form&gt; </code></pre> <p>ED: Using form method "post" caused the browser to throw a warning every time I hit the back button to get to the results page. I changed to "get" since the information is not sensitive and now it goes back w/o warning.</p>
    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.
 

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