2
I have a select HTML and change the value of a variable after passing the onchange() function. The problem is I have another condition and I can’t validate.
JS:
//function mode select
var option = 0
function getData(val) {
if (val.value === "option1"){
option = 1
alert("option deplacement!");
}else{
option = 2
alert("option ligne!");
}
show();
}
function show() {
console.log(this.option);
val = this.option
if (val == 2){
var el = document.getElementById('id_click');
el.onclick = function (e) {
alert("click2")
var ev = e || window.event;
var x2 = el.getAttributeNS(null, "x");
var y2 = el.getAttributeNS(null, "y");
//console.log(x2+" "+y2)
}
}
}
HTML:
<select id="selectid" onChange="getData(this);" >
<option value="option1">Deplacement</option>
<option value="option2">lignes</option>
</select>
<div id="id_div">dfsd</div>
Example in Jsfiddle
I edited using this code, I want an onclick Event in show function.
– akm
@akm The show function was created only to show that the
option
has been changed and is visible anywhere in the scope. Create an eventonclick
assigning to the element you want and use thethis.option
to validate the value in the function.– Lucas Fontes Gaspareto
I have a div, and when I click on it the onclick event appears. Inside the function I cannot identify the element. I just want to click when the select is 2.
– akm
@akm prevent clicking on an element is not possible, unless you disconnect the user’s mouse, joke. So... if your intention is to perform an action by clicking on
div
only whenoption
for 2 just make oneif
to validate. I changed the code, take a look at jsFiddle if you prefer.– Lucas Fontes Gaspareto