5

I have the following code written in HTML:

    <table cellspacing="0" cellpadding="0" width="100%" class="HeaderTable">
    <tbody><tr>
        <td id="dnn_ctr6707_TimeTableView_TdClassesList" class="HeaderClassesCell">
            class
            <select name="dnn$ctr6707$TimeTableView$ClassesList" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;dnn$ctr6707$TimeTableView$ClassesList\&#39;,\&#39;\&#39;)&#39;, 0)" id="dnn_ctr6707_TimeTableView_ClassesList" class="HeaderClasses">
            <option selected="selected" value="14">a</option>
            <option value="15">b</option>
            <option value="16">c</option>
            <option value="17">d</option>
            <option value="49">e</option>
            <option value="60">f</option>
        </select></td>

What i'm trying to do, is programmly click the option 'b' In the console tab. for some reason it doesn't work for me altho it seems good I think.

Here's what I tried:

var x = document.getElementById('dnn_ctr6707_TimeTableView_ClassesList') // this part work
var y = x.options[1].click() // I managed to get the text of the option but I want it to perform click and it doesn't work that way :(

Thank you for your help!

5
  • Why do you want your program to click the option 'b'? Commented Nov 30, 2018 at 19:46
  • 1
    Possible duplicate of How do I programmatically set the value of a select box element using JavaScript? Commented Nov 30, 2018 at 19:47
  • Try x.selectedIndex = 1; Commented Nov 30, 2018 at 19:47
  • Or x.selected = true. Commented Nov 30, 2018 at 19:47
  • 1
    @Thefourthbird Thanks it works also, but I found pew007 answer to be shorter :) Commented Nov 30, 2018 at 20:00

3 Answers 3

7

I think what you want to do is set the value of the select instead of clicking on the option.

Try this instead:

document.getElementById('dnn_ctr6707_TimeTableView_ClassesList').value = "15"
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks!, Shortest answer I saw :)
@Z.Zadon Glad to help
@pew007 this changes the select value fine but when submitting form it does not validate the field as correct. Seems it detects the element has not really been changed and some event is missing. Any ideas how to solve that?
4

Here is some simple code that lets you enter an index into a text field and the pick that index from the <select> element.

var btn = document.querySelector('#select');
var inp = document.querySelector('#inp');
var sel = document.querySelector('#dnn_ctr6707_TimeTableView_ClassesList');

btn.addEventListener('click', function() {
  var val = parseInt(inp.value, 10);
  sel.selectedIndex = val;
});
class
<select name="dnn$ctr6707$TimeTableView$ClassesList" id="dnn_ctr6707_TimeTableView_ClassesList" class="HeaderClasses">
<option selected="selected" value="14">a</option>
<option value="15">b</option>
<option value="16">c</option>
<option value="17">d</option>
<option value="49">e</option>
<option value="60">f</option>
</select>
<hr/>
<input id="inp" type="text" value="0"/>
<button id="select">Select</button>

Comments

2

I think what your asking is for an event to be activated. Javascript doesnt have a .click() function i believe.

var x = document.getElementById('dnn_ctr6707_TimeTableView_ClassesList');
var evt = new Event('click');
x.options[1].dispatchEvent(evt);

1 Comment

Well yes, Javascript does indeed have a click(). function, take a look here w3schools.com/jsref/met_html_click.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.