19

I have a simple html select tag with some options attached:

<select>
<option>school a</option>
<option>school b</option>
<option>school c</option>
</select>

I'd like to attach some simple event handlers to the options the same way I would to say... a link:

<option onclick="scheduleA();">school a</option>

Do I need to construct a separate Javascript function to deal with event handling in this situation or is there some quick html that will accomplish this task?

3
  • Take a look at onchange. You cuold write one function with parameter. Commented Feb 12, 2012 at 20:13
  • There your go my friend stackoverflow.com/questions/2000656/… Commented Feb 12, 2012 at 20:17
  • Thanks Alex, I didn't see this before I posted. Commented Feb 12, 2012 at 20:27

4 Answers 4

25

You would be better off assigning onChange="schedule(this.value); on the <select>. Partly because it actually works, partly to avoid redundant code if the same option is selected twice, partly because fewer event handlers is always better.

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

2 Comments

@kjarsenal: If you're supporting older versions of IE, just be sure to give each option element its own value attribute. Older IE won't automatically substitute the text content of the option when you don't give it a value attribute.
noted. those values will be generated dynamically by the framework. thank you.
14

Use an onchange event on the select

<select onchange="scheduleA.call(this, event)">

Then in your handler...

function scheduleA(event) {
    alert(this.options[this.selectedIndex].text);
}

DEMO: http://jsfiddle.net/hYY6X/

1 Comment

Thanks a lot, that's was helpfully
1

use onchange instead

<select onchange="schedule(this.value)">
<option>school a</option>
<option>school b</option>
</select>

function schedule(selectedValue){
     ... do something with selectedValue
}

1 Comment

That maybe, except the answer wasn't there when I loaded the page... If anything it re-enforces the fact it's a suitable solution. Moderators feel free to delete or whatever. I haven't been here that long but I really do get the impression there is a bit of an attitude I dislike from the community - hardly encouraging
0

This could be easily done:

<span class="text-left" hidden>{{ fullNameExists(concern.user) }}</span>

It will invoke it. You can even hide it with hidden preventıng result being visible to users.

<client-only>
<span class="text-left" hidden>{{ fullNameExists(concern.user) }}</span>
</client-only>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.