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?
Calling a function using external JavaScript file Once the JavaScript file is created, we need to create a simple HTML document. To include our JavaScript file in the HTML document, we have to use the script tag <script type = "text/javascript" src = "function.
To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.
Have you tried the setInterval() javascript function? It will automatically execute your code in a specified time(in milliseconds). setInterval( [you code or function call][, time interval in milliseconds] );
Call a JavaScript function from an HTML button click event getElementById() method. Now once you click on the button, the test() function will be executed and the alert box will be displayed in the browser.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With