Display another DIV when submit button is clicked

AKIWEB

This is my script

<script type="text/javascript">
           function showlevel1() {
               var l1 = document.getElementById('default');
               l1.style.display = 'block';
               var l2 = document.getElementById('secondDiv');
               l2.style.display = 'none';
           }

           function showlevel2() {
               var l2 = document.getElementById('secondDiv');
               l2.style.display = 'block';
               var l1 = document.getElementById('default');
               l1.style.display = 'none';

           }
           function selectHandler(select) {

               if (select.value == 'count') {
                   showlevel2();

               } else if (select.value == 'Top') {
                   showlevel1();
               } 
           }
</script>

HTML:

<form>
<select id='type'>
<option value="Top">TOP</option>
<option value="count">COUNT</option>
</select>

<input type='submit' value='submit' onclick=selectHandler(this)>
</form>

<div id='default' style="display:block">
    <h1>Div 1</h2>
</div>

<div id='secondDiv' style="display:none">
    <h2> Div 2</h1>
</div>

When I load the page the DIV 1 does shows up, since the property is set to display:'Block' but when I perform the selection from drop down it never changes to DIV2 upon the Count selection from drop down and a submit click button?

Can anyone please help in figuring out the issue and let me know what am I doing wrong?

Thanks

niko

Your select.value outputs submit since your this element referes to

-> <input type="submit" value="submit"/>

Why

selectHandler(this) is triggered by your submit button and not by your <select> tag, so when you perform select.value it returns submit as your text.

After seeing your question I guess you are in need of getting the select tag value.

Try this

function selectHandler(select) {
   // get your drop down list element i.e select tag
   var selected = document.getElementById('type');  
   if (selected.value === 'Count') {
      showlevel2();
   } 
   else if (selected.value === 'Top') {
      showlevel1();
   } 
}

After seeing your comments

The problem is your using type="submit" which posts to the same page when your click on it. Change it to type="button"

<input type='button' value='submit' onclick="selectHandler()">

function selectHandler() {
   // get your drop down list element i.e select tag
   var select = document.getElementById('type');  
   if (select.value === 'Count') {
      showlevel2();
   } 
   else if (select.value === 'Top') {
      showlevel1();
   } 
}

Or

<input type='submit' value='submit' onclick="return selectHandler()">

function selectHandler() {
   var select = document.getElementById('type');  
   if (select.value === 'Count')
      showlevel2();
   else if (select.value === 'Top') 
      showlevel1();
   return false;
}

js fiddle here http://jsfiddle.net/QGsza/68/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Hide divs and display div when button clicked

form does not submit, nothing happens when submit button is clicked

How to scroll to a particular div after reloading the page when the submit button is clicked?

ES6 Display div to none when button is clicked

On click radio button append value to div and remove from div when another radio button is clicked

Function to change style.display of a div when another is clicked

Jquery form validation when submit button is clicked

How to display the state on the same page when clicked Submit button in react

(React) How to get the state of another component when a submit button is clicked?

error blinks when submit button is clicked

Display a frame when a button is clicked

How to prevent form submit when a button clicked?

HTML button changes into another button when clicked on the same div

how to display a div only when a button is clicked

Trying to make a button display something when another button is clicked (C#)

How to get value of generated row value for another action when generated submit button is clicked

Display Content of Button When Button was Clicked

How to redirect to homepage when the submit button is clicked?

Display an image when submit button is clicked

how can i add css style for a div when i clicked on a button in another div

Button disappears when another is clicked

Trying to fill an array when a submit button is clicked

if submit button clicked modal popup open in middle of display?

Submit button no function when is clicked

How to submit a form from another component when the modal OK button is clicked (bootstrap vue)

How to submit a form when a different button is clicked

How to toggle a button when another button is clicked?

How to move a button from one div to another when clicked in reactjs?

Is there a script to replace a button with another button when clicked?