2

when clicking this select element, the inline call "onchange" should call a javascript function:

<select onchange="if (this.selectedIndex) do_something();">
  <option value=1>1</option>
  <option value=2>2</option>
</select>

basically how do i submit the option selected to 'do_something()' function? or in the function how can I obtain the option value selected?

2

2 Answers 2

2

you may try this :

function do_something(e) {
  alert(e);
}
<select onchange="if (this.selectedIndex) do_something(this.value);">
  <option value=1>1</option>
  <option value=2>2</option>
  <option value=3>3</option>
  <option value=4>4</option>
</select>

You may notice that i won't work for 1 because of selectedIndex which is equal to 0 for the first index (the value 1)

so more generic it can be :

function do_something(e) {
  //we make do test on the element e
  alert(e.value);
}
<select onchange="do_something(this);">
  <option value=1>1</option>
  <option value=2>2</option>
  <option value=3>3</option>
  <option value=4>4</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

1

function do_something(i){
  console.log(i);
}
<select onchange="do_something(this.selectedIndex);">
  <option value=1>1</option>
  <option value=2>2</option>
</select>

Try adding it as a parameter in the do_something function.

Comments

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.