Change the content of a div based on selection from dropdown menu

后端 未结 6 635
粉色の甜心
粉色の甜心 2020-11-30 00:14

Is there a simple way, using JavaScript, to dynamically show/hide content in a

based on the users selection from a drop down menu? For example, if a
6条回答
  •  情书的邮戳
    2020-11-30 00:40

    With zero jQuery

    
    
        
            
        
        
        
            
    
            
    Content 1
    Content 2
    Content 3

    Codepen

    document
      .getElementById('target')
      .addEventListener('change', function () {
        'use strict';
        var vis = document.querySelector('.vis'),   
          target = document.getElementById(this.value);
        if (vis !== null) {
          vis.className = 'inv';
        }
        if (target !== null ) {
          target.className = 'vis';
        }
    });
    .inv {
        display: none;
    }
    
    
        
        
            
        
        
            
    
            
    Content 1
    Content 2
    Content 3

提交回复
热议问题