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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kjarsenal
  • 934
  • 1
  • 12
  • 35

4 Answers4

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • @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. –  Feb 12 '12 at 20:27
  • noted. those values will be generated dynamically by the framework. thank you. – kjarsenal Feb 12 '12 at 20:29
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

use onchange instead

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

function schedule(selectedValue){
     ... do something with selectedValue
}
dannix
  • 235
  • 2
  • 13
  • 1
    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 – dannix Feb 12 '12 at 20:31
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>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Bitfinicon
  • 1,045
  • 1
  • 8
  • 22