JavaScript onchange function doesn't work

我与影子孤独终老i 提交于 2019-11-29 16:48:18

Covering your whole Question, I think you want like this:

Updated the whole Answer:

HTML

<div class="radio col-xs-2" id="populateSelection" onchange="changeSelection()">
      <label>
        <input type="radio" name="optradio" id="selectionTables" />Use Tables
      </label>&nbsp;&nbsp;
      <label>
        <input type="radio" name="optradio" id="selectionViews" />Use Views
      </label>
    </div>

Removed content inside ul and dynamically adding it:

<div class="dropdown col-xs-10">
      <!--DROPDOWN BUTTON-->
      <button class="btn btn-default dropdown-toggle btn-xs btn-orange" type="button" id="dropdownMenuSelect" data-toggle="dropdown" aria-expanded="true" style="margin-top:10px">
        Views
        <span class="caret"></span>
      </button>
      <ul id="dropdown-menu" class="dropdown-menu" role="menu" aria-labelledby="dropdownMenuSelect"></ul>
    </div>

JS/JQuery

function changeSelection() {

      if (document.getElementById("selectionTables").checked) {
        var x = document.getElementById("dropdownMenuSelect");
        var text = "Tables ";
        text += "<span class='caret'>";
        text += "</span>";
        x.innerHTML = text;
        document.getElementById("populateCheckBoxes").innerHTML = "";
        var y = document.getElementById("dropdown-menu");
        var text2 = "<li role='presentation'><a role='menuitem' tabindex='-1' class='link-no-jump' href='#graphOneChart'>";
        text2 += "Chart 1";
        text2 += "</a></li";
        text2 += "<li role='presentation'><a role='menuitem' tabindex='-1' class='link-no-jump' href='#graphOneChart'>";
        text2 += "Data 1";
        text2 += "</a></li";
        y.innerHTML = text2;
      } else {
        var x = document.getElementById("dropdownMenuSelect");
        var text = "Views ";
        text += "<span class='caret'>";
        text += "</span>";
        x.innerHTML = text;
        document.getElementById("unpopulateCheckBoxes").innerHTML = "";
        var y = document.getElementById("dropdown-menu");
        var text2 = "<li role='presentation'><a role='menuitem' tabindex='-1' class='link-no-jump' href='#graphOneChart'>";
        text2 += "Chart 2";
        text2 += "</a></li";
        text2 += "<li role='presentation'><a role='menuitem' tabindex='-1' class='link-no-jump' href='#graphOneChart'>";
        text2 += "Data 2";
        text2 += "</a></li";
        y.innerHTML = text2;
      }
    }

WORKING:CODEPEN

Change your function to this

function changeSelection() {

    if (document.getElementById("selectionViews").checked) {
        document.getElementById("dropdownMenuSelect").innerHTML = "Views";
        document.getElementById("populateCheckBoxes").innerHTML = "";
    } else {
        document.getElementById("dropdownMenuSelect").innerHTML = "Tables";
        document.getElementById("unpopulateCheckBoxes").innerHTML = "";
    }
}

Re-read this line:

var x = document.getElementById("populateSelection").id;

That's some pretty pointless logic. The ID of an element that you find with the ID populateSelection is always going to be populateSelection.

You should be using value attributes, and proper event handling.

DEMO

function changeSelection(e) {
  var x = e.target.value;
  if (x == "selectionViews") {
    document.getElementById("dropdownMenuSelect").innerHTML = "Views";
    document.getElementById("populateCheckBoxes").innerHTML = "";
  } else {
    document.getElementById("dropdownMenuSelect").innerHTML = "Tables";
    document.getElementById("unpopulateCheckBoxes").innerHTML = "";
  }
}

document.getElementById('populateSelection').addEventListener('change', changeSelection);
<!-- Latest compiled and minified CSS --><link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>

<form role="form">
  <div class="row">
    <div class="radio col-xs-2" id="populateSelection">
      <label>
        <input type="radio" name="optradio" value="selectionTables" />Use Tables
      </label>&nbsp;&nbsp;
      <label>
        <input type="radio" name="optradio" value="selectionViews" />Use Views
      </label>
    </div>
    <div class="dropdown col-xs-10">
      <!--DROPDOWN BUTTON-->
      <button class="btn btn-default dropdown-toggle btn-xs btn-orange" type="button" id="dropdownMenuSelect" data-toggle="dropdown" aria-expanded="true" style="margin-top:10px">
        Views
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenuSelect">
        <!--DROPDOWN MENU-->
        <li role="presentation"><a role="menuitem" tabindex="-1" class="link-no-jump" href="#graphOneChart">Chart</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" class="link-no-jump" href="#graphOneData">Data</a></li>
      </ul>
    </div>
  </div>
  <div class="row" id="populateCheckBoxes"></div>
  <div class="row" id="unpopulateCheckBoxes"></div>
</form>

Some reading material:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!