Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger event when selecting in a dropdown javascript eventlistener

I have this javascript function,

function listQ(){
 var e = document.getElementById("list");
 if(e.selectedIndex > 0){
  if("Blank Test" === e.selectedIndex){ alert("yo"); }
 }
}

My problem is how to trigger the function when selected the same value of dropdown list?

I tried this code,

document.getElementById("list").addEventListener("change",listQ);

And it must use an event listener.

like image 336
John Arellano Avatar asked Mar 26 '15 02:03

John Arellano


People also ask

Which event is triggered when the user select a menu?

The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse button, or by pressing the context menu key.

Which event is called on selection of an item from a dropdown list?

The HTML Select DropDownList has been assigned a jQuery OnChange event handler. When an item is selected in the HTML Select DropDownList, the jQuery OnChange event handler is executed within which the Text and Value of the selected item is fetched and displayed in JavaScript alert message box.

Can JavaScript trigger events?

HTML events are handled by JavaScript. When an event occurs, it requires some action to be taken. This action can be accomplished through JavaScript event handlers. In addition to JavaScript, jQuery which is equivalent to JavaScript in terms of functionality can also be used to trigger events in a HTML document.


2 Answers

You can use the onchange method on your select element.

var changedText = document.getElementById('changed');
function listQ(){
    changedText.textContent = this.value;
}
document.getElementById("list").onchange = listQ;

For this HTML

<select id="list">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
  <option value="4">Four</option>
</select>
<p>
  Your value <span id="changed"></span>
</p>

Here is a Fiddle forked from Jonathan, and here is MDN documentation for the change event.

like image 190
Tommaso Boggia Avatar answered Oct 20 '22 05:10

Tommaso Boggia


The line "Blank Test" isn't going to work because e.selectedIndex is going to be an index (a number). You could use e.options[e.selectedIndex].value

Apart from that, just change the event listener to "click" instead of "change":

<select id="list">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
  <option value="Blank Test">Four</option>
</select>

<script type="text/javascript">
  function listQ(){
    var e = document.getElementById("list");
    if(e.selectedIndex > 0){
      if("Blank Test" === e.options[e.selectedIndex].value){ alert("yo"); }
    }
  }
  document.getElementById("list").addEventListener("click",listQ);
</script>

Here's a Fiddle to demonstrate: https://jsfiddle.net/h81pcpm0/

like image 23
Jonathan Avatar answered Oct 20 '22 05:10

Jonathan