I have this select object:
<select id="alunos" onchange="arteAluno(this);">
<option selected="selected"></option>
<option value="loadImage();">Aluno 1</option>
<option value="../Index.html">Aluno 2</option>
<option value="../Index.html">Aluno 3</option>
</select>
and this function:
function arteAluno (sel) {
var url = sel[sel.selectedIndex].value;
window.location = url;
}
and I was making the page change with this. But now I want to get this code:
$(document).ready(function() {
function O(obj) {
return document.getElementById(obj);
}
function loadImage() {
O('arte').style.backgroundImage="url(aikido.jpg)";
O('arte').style.width = '200px';
O('arte').style.height = '160px';
}
})
and make this function loadImage only be triggered when I select one of the options of the tag.
Since you are already using jQuery, you might just as well use it for everything here. You only need to bind one
.change()handler which setswindow.locationif one of the URLs is chosen, and executes theloadImage()function otherwise.Rather than call the function name
loadImage()in the<option>value (which looks strange), I have replaced it below with a special valuevalue='load_image'. The.change()handler will look for that value and execute the function if found.And the jQuery: