I have currently got a 2-tier javascript drop down box attached to my form, and i am looking to add another 3-tiers, but completely seperate to my current javascript and can't unfortunately figure it out (quite new to javascript :( )
Here what I have so far, it all works ( not even sure what framework to select on fiddle for it to display properly lol :( the embarassment haha
Any help is appreciated <3
This will probably be enough code for you to get the idea.
jsFiddle here
I used jQuery to get handles to the various DOM elements. Here is a quite decent resource for browsing all jQuery selectors, events, effects, css, etc - despite the source. (Just keep hitting Next Chapter)
First, this is your code for catching the optone select element's change event, removed from inline javascript (which is never a good idea):
$('select[name=optone]').change(function() {
var selval = $(this).val();
setOptions(selval);
});
Simple enough, yes?
I left your first setOptions function as is, because it works. However, I wrote the next function setOptions2() in jQuery, to show you how much less typing is required.
To catch the next select element's change event:
$('select[name=opttwo]').change(function() {
var sel2val = $(this).val();
alert('You selected: ' + sel2val);
setOptions2(sel2val);
$('#selthreeDiv').show();
});
Notice in my jsFiddle that I added a hidden div containing the 3rd select control, and I display that control when the 2nd select is changed...
Hopefully this will be of assistance.
ok I did something like this and it worked for me.
your new javascript
function setOptions2(chosen) {
var selbox = document.myform.optthree;
selbox.options.length = 0;
if (chosen == "1") {
selbox.options[selbox.options.length] = new Option('Worked', ' ');
}
}
then added some new html
<select name="opttwo" size="1" onchange="setOptions2(document.myform.optone.options[document.myform.optone.selectedIndex].value);" >
<option value="Please Select Prompt Category" selected="selected">Please Select Prompt Category</option>
</select>
<select name="optthree" size="1">
<option value="Please Select Prompt Category" selected="selected">Please Select Prompt Category</option>
</select>
Related
I'm trying to fill a form using Selenium, but the form has a disabled field.
Disabled field
The field is only editable when I modify the field above it.
Open field
When I set the value directly using the code below, the field is not open for editing
js.executeScript("document.getElementById('field_id').value='" + brand + "'");
Example
I tried to simulate the click in the field, press the tab key, press the enter key, but none had any effect.
Is there any way for me to trigger the same event that the user is performing on the screen to release the field through selenium or javascript?
In the HTML code, the options are not listed, so the options are loaded from a javascript function that is executed after filling the first field
Options
Because I really liked it I'll copy Tschallackas Intro:
Your test is flawed. You are not following user behaviour.
Sadly I totally disagree with the rest of the answer :(
I would like to ask WHY are you trying to use JavaScript?
Is this something a real User would do? I really doubt it!
The crucial thing with End2End-Tests is to simulate your User behaviour as close as possible. Therefore I would suggest to use the Webdriver to do things like that in your Seleniumtest.
Select dropdown = new Select(webdriver.findElement(By.id("field_id")));
dropdown.selectByVisibleText("ONESOURCE");
(Assuming you are using Java by the tag on your question)
Your test is flawed. You are not following user behaviour.
You are doing:
js.executeScript("document.getElementById('field_id').value='" + brand + "'");
Which tries to change a value on a dropdown. This doesn't work because dropdowns work via a selectedIndex, which you can use to get the correct value from the options collection on the dropdown element.
Also, when a user changes a value, a change event is triggered, which notifies other scripts that listen to that event that something has changed. You need to emulate this too to trigger your change script.
js.executeScript("let select = document.getElementById('field_id');"+
"select.selectedIndex = 1;/* change this to the value corresponding to the correct index of the value you wish to test. */"+
"select.dispatchEvent(new Event('change'));");
See the example below for how the javascript should work.
document.getElementById('field_id').addEventListener('change', (e) => {
if(e.target.options[e.target.selectedIndex].value > 1) {
document.getElementById('the_disabled').disabled = false;
}
else {
document.getElementById('the_disabled').disabled = true;
}
});
document.getElementById('trigger').addEventListener('click',() => {
let select = document.getElementById('field_id');
select.selectedIndex = 1;// change this to the value corresponding to the correct index of the value you wish to test.
select.dispatchEvent(new Event('change'));
});
<select id="field_id">
<option value="1">--none--</option>
<option value="2">COMPANY A</option>
<option value="3">COMPANY B</option>
</select>
<BR/>
<select id="the_disabled" disabled="disabled">
<option value="0">--none--</option>
<option value="1">SELECT A</option>
<option value="2">Select B</option>
</select>
<BR/>
<button id="trigger">Trigger selenium emulation</button>
I want to be able to validate my drop down lists.
There are 6 destinations, and if the user was to select Cardiff in the "Departure" drop down, then if they was to select Cardiff again in the "Destination" drop downn, it should give a message box saying 'Destination can't be the same as Departure'.
This needs to be done in HTML and JavaScript/JQuery.
Thanks
Departure: <br> <select>
<option value="" disabled selected>Please Select</option>
<option>Birmingham</option>
<option>Bristol</option>
<option>Cardiff</option>
<option>Liverpool</option>
<option>London</option>
<option>Manchester</option>
</select><br><br>
Destination: <br><select>
<option value="" disabled selected>Please Select</option>
<option>Birmingham</option>
<option>Bristol</option>
<option>Cardiff</option>
<option>Liverpool</option>
<option>London</option>
<option>Manchester</option>
</select><br><br>
Very quick example but essentially you can do something like this:
Fiddle: http://jsfiddle.net/6hojsqhr/
$('#select1, #select2').on('change',
function () {
if ($('#select1').val() == $('#select2').val()) {
// Display message
}
else {
// Remove message if displayed
}
}
);
Well I am feeling slightly called out so let me explain what is happening here.
$('#select1, #select2').on('change',
First I select the two inputs we want to test against and attach a change event to them. This will fire this event every time the input's value changes and loses focus.
if ($('#select1').val() == $('#select2').val()) {
// Display message
}
else {
// Remove message if displayed
}
In this function we get both values of the two inputs we want to test for equivalent values. If they are the same we display the message. If not don't do anything or in some cases remove a displayed message.
Again this is a very quick solution that can be much more dynamic and thought out depending on your specific scenario. This is very simple JavaScript/jQuery. I would recommend doing some reading and perhaps free courses online that can teach these basics. A simple google search will pull up a ton of examples of code like this and websites that teach how to do this.
I am just starting out with some java script in an asp.net mvc web site.
I current have a form which I am working on.
The first field which the user is prompted with is a combobox / select (in html)
here is the code for it:
<select name="select">
#foreach (var item in Model.networks)
{
<option value="">#Html.DisplayFor(modelItem => item.name)</option>
}
</select>
Now my next field depends on the option which they chose from the combo box.
How can I populate the next field based on the option they chose in the combo box?
So when the user navigates to the page they will ave a combo box populated with all the options. Below that will be empty fields. When the user selects a option in the combo box I want it to then populate the empty fields with the corresponding data from the option which was chosen.
How do I go about doing this?
Please give the newby answer as in the method in which it will be done. I am assuming that I will be using java script for it?
Although I cannot understand your question in detail, I hope I can help you.
HTML
If you have a select element that looks like this:
<select id=dropdown>
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
Plain Javascript solution
Running this code:
var element = document.getElementByID('dropdown');
var current = e.options[e.selectedIndex].value;
Would make current be 2. If what you actually want is test2, then do this:
var e = document.getElementById('dropdown');
var strUser = e.options[e.selectedIndex].text;
Which would make current be test2
Put the onChange="getSelectedValue" attribute of the select element and then use the following javascript.
<script>
function getSelectedValue(sel)
{
txtToFill.Text(sel.options[sel.selectedIndex].text);
}
</SCRIPT>
If I understand your question correctly you want to react to a combo box value changing and display different content in your page.
If that is the case what you need to know is
how to handle the change event in the select (drop down)
populate empty fields
Here's how you can register and handle the change event in the dropdown:
$("select[name='select']").on("change", function(){
$('#input1').val("What you want in the input goes here");
});
Here's a fiddle that demonstrates this.
I've been searching for an answer to this question for quite some time now, with no luck, or buggy solutions at max.
The problem im facing is that i have a select element which (obviously) doesn't fire the "onchange" event when selecting the already selected item.
Like this:
<select onchange="alert(this.value);">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
Now, say i select item 1 first.
Then afterwards i need to be able to select item 1 again.
This feature is extremely important to the success of the project I'm working on.
Any help is greatly appreciated.
Edit: (More info)
This functionality is needed as im working on a project with a google maps where users are presentet with a dropdown to quickly jump to a country (Eg you select "Spain" in the dropdown, google maps sets spain as your view.
the problem comes when you want to go to spain, then drag around the map, and end up in italy. Now you want to go back to spain, but you cant select it from the dropdown until you select something else first. My boss doesn't like that :)
Edit2: Solution
So by now theres a few solution.
One of them is to throw the action onblur (when unfocusing the control) this could work, as i could blur the list onchange, but still for people selecting the same item again, the trigger to blur the control wont go, and unless they switch focus by them self, they wont see the changes to the map.
Still i cannot understand why it should be so hard to find some event that excutes on option select / click / blur or whatever.. ill keep looking myself, and check back here a tad later.
Edit 3: Final solution
Right so i finally managed to get this working, not exactly as it was ment to be, but close enough, atleast for now anyways.
The solution i came up with was to add a "Please select country" option at the top of the select.
Then on change, i would change the text, and value of "Please select country" to that of the selected item, and reset the selectedindex to 0.
This way, when selecting, say, Spain, it will be at the top of the list in a disabled state, and further down in a working state. so now you can click spain in the middle of the list to go back to spain even though it is still selected (the top item is)
Quite neat, idea was supplied from a coworker.
script is as following:
var dummyOption;
function setdummyCountry(obj)
{
var selectedOption = obj.options[obj.selectedIndex];
if (dummyOption != null) {
dummyOption.text = selectedOption.text;
dummyOption.value = selectedOption.value;
}
else {
dummyOption = document.createElement("option");
dummyOption.text = selectedOption.text;
dummyOption.value = selectedOption.value;
dummyOption.setAttribute("disabled", "true");
obj.options[0] = dummyOption;
}
obj.selectedIndex = 0;
}
<select onchange="setdummyCountry(this);">
<option value="">Please select country</option>
<option value="ES">spain</option>
<option value="SE">sweden</option>
<option value="NO">norway</option>
</select>
i hope this will help someone!
And to those who tried to help me thank you for your ideas and time.
I think there is no way change event is gonna fire if the same item is selected. We were facing the same issue so what we did was a simple usability hack: When an item is selected we show to the user the item selected in a span and reset the value of dropdown to its default value (--Select--).
HTH
this is maybe not excactly what do you want, but this another alternative that you can decide.
after select the option, get it back to default(the empty one/first) option
<select onchange="alert(this.value);this.value='';">
<option value='' selected='selected'></option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
or you can make
<select onblur="alert(this.value);">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
but this way will call your function (alert) when user leave/blur/unfocus the list :p
I was looking for the solution for the same scenario and ended up writing a angularjs directive for the same -
guthub link - angular select
Used element[0].blur(); to remove the focus off the select tag. Logic is to trigger this blur on second click of the dropdown.
as-select gets triggered even when user selects the same value in the dropdown.
DEMO - link
you can use onclick function and maintain a count for that, once you get same target.value twice, then you can perform your onchange fuctionality there.
I have implemented this in Vue.js
Select block:
<select
id="status"
name="status"
#change="
onChangeStatus(un.id, $event)
"
#click="
onChangeStatus(
un.id,
$event,
'same'
)
"
class="form-control"
:value="un.status"
>
<option value="Uncleaned">Uncleaned</option>
<option value="Partially Taken for cleaning">
Partially Taken for cleaning
</option>
<option value="Fully Taken for cleaning">
Fully Taken for cleaning
</option>
</select>
Kept count=0 initially.
I have used same function on both events, checked conditions and returned data accordingly
onChangeStatus(id, e, type) {
if (type === "same") {
if (e.target.value === "Partially Taken for cleaning") {
this.count += 1;
if (this.count === 2)
this.$router.push({
name: "NewPage",
params: {
id: id,
status: e.target.value,
},
});
}
} else {
this.$router.push({
name: "NewPage",
params: {
id: id,
status: e.target.value,
},
});
}
},
I am wondering if anyone has any experience using a JQuery plugin that converts a html
<select>
<option> Blah </option>
</select>
combo box into something (probably a div) where selecting an item acts the same as clicking a link.
I guess you could probably use javascript to handle a selection event (my javascript knowledge is a little in disrepair at the moment) and 'switch' on the value of the combo box but this seems like more of a hack.
Your advice, experience and recommendations are appreciated.
The simple solution is to use
$("#mySelect").change(function() {
document.location = this.value;
});
This creates an onchange event on the select box that redirects you to the url stored in the value field of the selected option.
I'm not sure where you want to link to when you click the Div, but given something like this perhaps would work:
<select id="mySelect">
<option value="1">Option 1</option>
<option value="2">Option 2</options>
</select>
<div id="myDiv"/>
and the following JQuery creates a list of <div> elements, a goes to a URL based on the value of the option:
$("#mySelect option").each(function() {
$("<div>" + $(this).text() + "</div>").appendTo($("#myDiv")).bind("click", $(this).val(), function(event) {
location.href = "goto.php?id=" + event.data;
});
});
$("#mySelect").remove();
Does this do what you want?
If you're going to have a lot of select boxes that you want to allow to use as redirects, without having to define them independently, try something similar to:
$("[id*='COMMON_NAME']").change(function() {
document.location = this.value;
});
And have your select boxes be named accordingly:
<select id="COMMON_NAME_001">...</select>
<select id="COMMON_NAME_002">...</select>
This creates a onchange event for all IDs containing "COMMON_NAME" to do a redirect of the <option> value.
This bit of javascript in the 'select':
onchange="if(this.options[this.selectedIndex].value!=''){this.form.submit()}"
It's not ideal (because form submissions in ASP.NET MVC which I'm using don't appear to use the routing engine for URLs) but it does its job.