lets say I have an option:
<select class="opt">
<option value="1">1</option>
<option value="2">2</option>
</select>
<div id="d">-it will be overwritten-</div>
and a jQuery binds to in, and add a new option:
$('.opt').change(function() {
alert ('changed!');
$('#d').html ('<select class="opt"><option value="11">12</option><option value="22">22</option></select>');
});
and this stops working. I tried to re-attach the events (not seen in the code) still no work.
http://jsfiddle.net/p9hhue1L/
Related
I have this simple select menu and its options:
<select name="selectMenu" id="selectMenu">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
I want to get the value from the selected option automatically so I tried this:
let selectMenu = document.getElementById("selectMenu");
let scoreLimit = selectMenu.options[selectMenu.selectedIndex].value;
and with .text instead of .value too, no difference.
and I tried let scoreLimit = selectMenu.value; right away too, no difference
The problem is if you reload the page and the selected option is 1, for example, the variable scoreLimit will always be 1 even if I select a different option afterward from the select menu. I want it to update its value automatically without reloading the page as I select different options, how can I do this? (pure JS only if possible)
It would be best to use a function
const getScoreLimit = () => document.getElementById("selectMenu").options[selectMenu.selectedIndex].value
and call it when you need (just like normal function)
getScoreLimit()
You can create a function and pass that function as callback function of change event of the element like the following way:
let selectMenu = document.getElementById("selectMenu");
function selectValue(el){
let scoreLimit = el.options[el.selectedIndex].value;
console.log(scoreLimit);
}
selectMenu.addEventListener('change', function(){selectValue(this)});
selectValue(selectMenu); // call this for the default value on page load
<select name="selectMenu" id="selectMenu">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
In the javascript I see the following defined:
$('#dropDownId').change(function(){
... do stuff...
});
<select id="dropDownId">
<option value="1">value 1</option>
<option value="2">value 2</option>
<option value="3">value 3</option>
</select>
I've tried using the following code:
webBrowser.Document.GetElementById("dropDownId").SetAttribute("value", "1");
webBrowser.Document.GetElementById("dropDownId").Children[1].SetAttribute("selected", "selected");
webBrowser.Document.GetElementById("dropDownId").InvokeMember("onchange");
I can see the drop down get changed to the right value, but the following never gets executed:
$('#dropDownId').change(function(){
... do stuff...
});
Also, when I look at the properties for "dropDownId" in Chrome, the "onchange" event is null, so how can I invoke the above "change" script for the dropdown?
jquery attaches events to allow for multiple event handlers, that's why onchange property of your select is null.
You can do:
function myChange(){
... do stuff...
}
<select id="dropDownId" onchange="myChange">
...
</select>
or
webBrowser.Document.InvokeScript("myChange");
or
function myChange_Jquery(){
$("#dropDownId").change();
}
webBrowser.Document.InvokeScript("myChange_Jquery");
I have come across <select> (drop down list) in HTML in the learning process. I learnt how to code a <select> drop down list.
How can I perform a particular action (in my case I am performing a mathematical operation) when a value in the <select> is chosen?
You can do this a couple of ways.
I would suggest putting a onchange event attribute and give the element an ID.
eg.
<select id ="example1" onchange="example_function()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Now that you have done that you can goto your JS and use the follow.
var droplist
var droplist_value
function example_function(){
droplist = document.getElementbyID("example1")
droplist_value = droplist.value
}
You can now use the droplist_value as you want.
I have the following markup:
<select onchange="jsFunction()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
When a user pulls down the combobox and selects the same option that was previously selected (or doesn't change the selection at all), JavaScript doesn't regard it as an onchange event. So, the jsFunction() is not called. But I want the jsFunction() called even in this case. How can I achieve this?
I'd do it like this:
<select onchange="jsFunction()">
<option value="" disabled selected style="display:none;">Label</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
If you want you could have the same label as the first option, which in this case is 1.
Even better: put a label in there for the choices in the box.
You have to add empty option to solve it,
I also can give you one more solution but its up to you that is fine for you or not Because User select default option after selecting other options than jsFunction will be called twice.
<select onChange="jsFunction()" id="selectOpt">
<option value="1" onclick="jsFunction()">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
function jsFunction(){
var myselect = document.getElementById("selectOpt");
alert(myselect.options[myselect.selectedIndex].value);
}
Just set the selectIndex of the associated <select> tag to -1 as the last step of your processing event.
mySelect = document.getElementById("idlist");
mySelect.selectedIndex = -1;
It works every time, removing the highlight and allowing you to select the same (or different) element again .
Try this. Just add an empty option. This will solve your problem.
<select onchange="jsFunction()">
<option></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
For this problem, I have finally put a new <i> tag to refresh the select instead. Don't try to trigger an event if the selected option is the same that the one already selected.
If user click on the "refresh" button, I trigger the onchange event of my select with :
const refreshEquipeEl = document.getElementById("refreshEquipe1");
function onClickRefreshEquipe(event){
let event2 = new Event('change');
equipesSelectEl.dispatchEvent(event2);
}
refreshEquipeEl.onclick = onClickRefreshEquipe;
This way, I don't need to try select the same option in my select.
use the "onmouseup" property with each option element. it's verbose, but should work. also, depending on what your function is actually doing, you could arrange things a little differently, assuming the number is important in the handler:
<select>
<option onmouseup="handler()" value="1">1</option> //get selected element in handler
<option onmouseup="handler(2)" value="2">2</option> //explicitly send the value as argument
<option onmouseup="handler(this.value)" value="3">3</option> //same as above, but using the element's value property and allowing for dynamic option value. you could also send "this.innerHTML" or "this.textContent" to the handler, making option value unnecessary
</select>
JavaScript code:
on mousedown event: set selectedIndex property value to -1
on change event: handle event
The only drawback is that when the user clicks on the dropdown list, the currently selected item does not appear selected
It's not firing because the value hasn't "changed". It's the same value. Unfortunately, you can't achieve the desired behaviour using the change event.
You can handle the blur event and do whatever processing you need when the user leaves the select box. That way you can run the code you need even if the user selects the same value.
I have a set of dropboxes
code is
<fieldset data-role="controlgroup" data-type="horizontal">
<!--<legend> </legend>-->
<select name="select-widget" id="select-widget">
<option value="Widget">Widget</option>
</select>
<select name="select-nbl" id="select-nbl">
<option value="NBL">NBL</option>
<option value="HSM">HSM</option>
<option value="TERR">TERR</option>
<option value="KEY_ACCOUNT">KEY ACCOUNT</option>
</select>
<select name="select-level-focus" id="select-level-focus">
<option value="LEVEL">LEVEL</option>
<option value="FOCUS">FOCUS</option>
</select>
<select name="select-wdg" id="select-wdg">
<option value="WDG">WDG</option>
</select>
<select name="select-wds" id="select-wds">
<option value="WDS">WDS</option>
</select>
</fieldset>
No I want to remove exixsting selectbox or add new select box to it on change of "select-nbl" or "select-levelfocus"
How to do it using jquerymobile
This has little to do with jQuery mobile, that's merely a mobile framework. The event listeners and DOM manipulation you want to do here is still handled by jQuery.
Anyways, how do you do it?
Well, you have your event listeners:
$('#select-nbl').change(function() {
#Do something
});
And you can hide stuff:
$('#select-wdg').parents('.ui-select').hide();
The use of parents() is to take care of the extra styling jQuery mobile puts in there.
You can't easily add select boxes to jQuery mobile, but if they're hidden, you can show them.
$('#select-wdg').parents('.ui-select').show();
All this is action: http://jsfiddle.net/luhn/Vgc4g/9/