add form field based on option selection - java

I have a form where i have to add form field based on options selected in the select box
what i have done is created all form fields and based on selection show hide fields , But is there any simplified way to do this
<select name="child" id="child">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="age1">
<select name="age1" id="age1">
//options
</select>
</div>
<div id="age2">
<select>
//options
</select>
</div>
<div id="age3">
<select>
//options
</select>
</div>
here is my jquery
$('#child').change(function() {
if( $(this).val() == 0 ) {
$("div#age1").hide();
$("div#age2").hide();
$("div#age3").hide();
$("div#age4").hide();
}
if( $(this).val() == 1 ) {
$("div#age1").show();
$("div#age2").hide();
$("div#age3").hide();
$("div#age4").hide();
}
if( $(this).val() == 2 ) {
$("div#age1").show();
$("div#age2").show();
$("div#age3").hide();
$("div#age4").hide();
}
if( $(this).val() == 3 ) {
$("div#age1").show();
$("div#age2").show();
$("div#age3").show();
$("div#age4").hide();
}
if( $(this).val() == 4 ) {
$("div#age1").show();
$("div#age2").show();
$("div#age3").show();
$("div#age4").show();
}
});
How to do it in simplified way and how to get post value for this

same basic approach as Prashant - but theres no need to use eq().. the divs all have ids that match the values of the select list - and note that the first select list in your current code have the same id as its parent div. This will cause an issue, but the following will fix it.
All this does is on the change of the #child select list - triggers all divs to hide (note the display none in the CSS to hide them initially) and then show the one that matches the age+value of child. I would not actually do it this way myself, but given the code you had this works. I also added options to the age select lists to give them something to show when you change the #child list.
As I said, I would not suggest doing it this way - I would suggest appending a new select list to the end of the form based on the selection -that way you dont have all select lists just sitting there and you don't have to worry about submitting the selected value from one of the hidden lists when you submit the form. Just a thought.
$('#child').change(function() {
$("div").hide();
$("#age"+$(this).val()).show();
});
div{display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="child" id="child">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="age1">
<select name="age1">
<option value="a">a</option>
<option value="b">b</option>
</select>
</div>
<div id="age2">
<select name="age2">
<option value="c">c</option>
<option value="d">d</option>
</select></div>
<div id="age3">
<select name="age2">
<option value="e">e</option>
<option value="f">f</option>
</select></div>
<div id="age4"> <select>
<option value="g">g</option>
<option value="h">h</option>
</select></div>

Try This:
<select name="child" id="child">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div class="myage" id="age1">
<select name="age1" id="age_1">
//options
</select>
</div>
<div class="myage" id="age2">
<select>
//options
</select></div>
<div class="myage" id="age3"> <select>
//options
</select></div>
Your JS :
$(".myage:eq(0)").show();
$('#child').change(function() {
$(".myage").hide();
$(".myage:eq("+$(this).val()+")") .show();
});
Use Class instead of ID :)

Related

How to access thymeleaf model attribute (A list from an object specifically) to populate an html dropdown list?

I've been doing some research and I an understand how to use specific objects if you passed the entire things in with the model.addAttribute("object name", object); but I don't know how to grab the data inside of the html if I only passed a method that returns a list with it.
So, this would be my controller for the class:
#Controller
public class randomNotification {
#Autowired
private randomService randomService;
#Autowired
private secondService secondService;
#GetMapping("/random-notification.html")
public String renderpage(Model model) {
model.addAttribute("orgs", randomService.getOrgs());
model.addAttribute("templates", secondService.getTemplates());
return "random-notification";
}
}
The randomService.getOrgs() would return a Collection of Strings like ["AB","BC","CD","DE","EF","FG"]
The secondService.getTemplates() would also return a similar collection of strings.
What I want to do is use each of the values in the list of strings to populate a drop down list inside my html. So if I had the following blank combobox:
<div class="random-combobox">
<th><label>Random </label></th>
<th>
<select class="random-cbox">
<option value="">Select one..</option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
</th>
</div>
I'd want to populate each option with one item from the list so it would almost be like
<div class="random-combobox">
<th><label>Random </label></th>
<th>
<select class="random-cbox">
<option value="">Select one..</option>
<option value="<getOrgsOption1>">AB</option>
<option value="<getOrgsOption2>">BC</option>
<option value="<getOrgsOption3>">CD</option>
<option value="<getOrgsOption4>">DE</option>
<option value="<getOrgsOption5>">EF</option>
<option value="<getOrgsOption6>">FG</option>
</select>
</th>
</div>
Something like this will do it
<select>
<option th:each="org : ${orgs}"
th:value="${org}"
th:text="${org}"></option>
</select>

Selenium Webdriver JAVA - Unable to select option from dropdown menu (JAVA)

I have currently hit a stumbling block when trying to simply pick an option from a dropdown menu whilst automating a website.
Here is a snippet of the HTML:
<select id="alarm-dropdown" name="alarm-dropdown" data-bind="value: AlarmCode" data-ga-category="CarInsurance_YourVehicle_VehicleDetails_FittedWithAnAlarm" data-ga-action="Selected" data-ga-label="CarInsurance_YourVehicle_VehicleDetails_FittedWithAnAlarm">
<option class="" selected="" disabled="" value="">Please select...</option>
<option class="" id="alarm-dropdown-99991" value="99991">Factory Fitted Thatcham Approved Alarm/Immobiliser</option>
<option class="" id="alarm-dropdown-99992" value="99992">Factory Fitted Thatcham Approved Alarm</option>
<option class="" id="alarm-dropdown-99993" value="99993">Factory Fitted Non-Thatcham Alarm/Immobiliser</option>
<option class="" id="alarm-dropdown-99994" value="99994">Factory Fitted Non-Thatcham Alarm</option>
<option class="" id="alarm-dropdown-#F" value="#F">Factory Fitted</option>
<option class="" id="alarm-dropdown-#N" value="#N">None</option>
</select>
Here is my current code: -
Select select = new Select(driver.findElement(By.id("alarm-dropdown")));
select.selectByValue("Factory Fitted Non-Thatcham Alarm");
I have tried XPATH/ID/CSS
Select select = new Select(driver.findElement(By.id("alarm-dropdown")));
select.selectByValue("99994");
Try with the xpath:
driver.findElement(By.xpath("//select[#id='alarm-dropdown']/option[#value='99994']")).click();

html load conditional value to select tag

I try to load conditional value to a select tag based on the user's choice on a pervious form tag. it might require java or php. i am not competent in any of those two language.
the logic is explain as the following.
<form>
<input name="complaint" value="copyright" type="radio">copyright <br>
<input name="complaint" value="trademark" type="radio">trademark <br>
<input name="complaint" value="other_concern" type="radio"> other concerns
</form>
if complaint="copyright" then
<select>
<option value="image">image</option>
<option value="product">product</option>
</select>
elseif complaint="trademark" then
<select>
<option value="item">item</option>
<option value="image">image</option>
</select>
elseif complaint="other_concern"
<select>
<option value="explain">explain</option>
</select>
end if
any solution?
Heres a solution in Jquery. Add a class name to the inputs and hide the selct options by default
<input name="complaint" value="copyright" type="radio" class="complaint">
then add unique ID tags to the select options along with a css class i like to use, .addClass and .removeClass are slighty faster and more efficent than .hide() and .show() selectors
Add the css at the bottom of your css sheet
.hidden{position:absolute;width:0;height:0;overflow:hidden;visibility:0;padding:0;margin:0}
<select id="copyright" class="selectOptions hidden">
<option value="image">image</option>
<option value="product">product</option>
</select>
Finally add some JQuery You need to copy the remainder for your other hidden values
$('.complaint').click(function() {
$('.selectOptions').addClass('hidden');
var checkComplaint = $(this).val();
$('#' + checkComplaint).removeClass('hidden');
});

How to set a specific value at the end of struts 2 select element?

Here is an example, with set the value Please_select_a_value at the beginning of the rendered select list:
<s:select id="myGuiId" name="myGuiName" headerKey= "0" headerValue="Please_select_a_value" list="mySourceList" />
This generated the following HTML code:
<select name="myGuiName" id="myGuiId">
<option value="0">Please_select_a_value</option>
<option value="1">Value_1</option>
<option value="2">Value_2</option>
<option value="3">Value_3</option>
<option value="4">Value_4</option>
...
<option value="20">Value_20</option>
</select>
Is there a way, to set the value Please_select_a_value at the end of the rendered select list (if I open select list in GUI) without using some tricks in the correspondent Struts action class? Something like this in HTML code:
<select name="myGuiName" id="myGuiId">
<option value="1">Value_1</option>
<option value="2">Value_2</option>
<option value="3">Value_3</option>
<option value="4">Value_4</option>
...
<option value="20">Value_20</option>
<option value="21">Please_select_a_value</option>
</select>

Selenium WebDriver_Dropdown listed Element not able to select

Hello im not able to find following dropdown element. Here is HTML code:
<div class="FormDetails"><br>
<ul class="lsn"><br>
<li><br>
<span id="errfileName"></span><br>
</li><br>
<li style="display: block;"><br>
<div class="LeftSection-Form" style="margin-bottom: 15px;"><br>
<div class="FormLabel"><br>
<Community<br>
<span class="FormDetails_required"> Required</span><br>
</div><br>
<div class="FormValue"><br>
<select id="Fact-Communities-LIVE" class="ToolTipPopup Sel1355" tooltip-title=<p>"Community "</p> onkeydown="return preventBackspace(event);" <br>onchange="dropDownValue(this);" name="Fact.Communities.LIVE" emptyoption="-Please select-"><br>
<option value="TxnyD.PleaseSelect.1.1">-Please select-</option><br>
<option value="TxnyD.Communities.1.1">UVDB</option><br>
<option value="TxnyD.Communities.2.1">THQS</option><br>
<option value="TxnyD.Communities.3.1">Master</option><br>
<option value="TxnyD.Communities.4.1">Connexio</option><br>
<option value="TxnyD.Communities.5.1">SL</option><br>
<option value="TxnyD.Communities.6.1">OG</option><br>
<option value="TxnyD.Communities.7.1">UT</option><br>
<option value="TxnyD.Communities.8.1">TR</option><br>
<option value="TxnyD.Communities.9.1">FGW</option><br>
<option value="TxnyD.Communities.10.1">E.ON TSMS</option><br>
<option value="TxnyD.Communities.11.1">Vattenfall TSMS</option><br>
<option value="TxnyD.Communities.12.1">Delivery1</option><br>
<option value="TxnyD.Communities.13.1">Test community</option><br>
<option value="TxnyD.Communities.14.1">Automotive</option><br>
<option value="TxnyD.Communities.15.1">SHELL SUPPLIER QUALIFICATION SYSTEM</option> <br>
<option value="TxnyD.Communities.17.1">Nestle</option><br>
<option value="TxnyD.Communities.18.1">BuildingConfidence</option><br>
</select><br>
</div><br>
</div><br>
i tried following script to find these element:
//driver.findElement(By.xpath(".//*[#id='Fact-Communities-LIVE']")).click();
//driver.findElement(By.xpath(".//*[#id='Fact-Communities-LIVE']/option[16]/text()='SHELL SUPPLIER QUALIFICATION SYSTEM'")).click();
tried these one as well:
//driver.findElement(By.id("Fact-Communities-LIVE")).sendKeys("SHELL SUPPLIER QUALIFICATION SYSTEM");
//Thread.sleep(1000);
//new Select(driver.findElement(By.id("Fact-Communities-LIVE"))).selectByVisibleText("SHELL SUPPLIER QUALIFICATION SYSTEM");
& these one as well
//WebElement dropDown = driver.findElement(By.xpath(".//*[#id='Fact-Communities-LIVE']"));
//List<WebElement> options = dropDown.findElements(By.xpath(".//*[#id='Fact-Communities-LIVE']/option[16]/text()"));
//driver.findElement(By.xpath(".//*[#id='Fact-Communities-LIVE']/option[16]")).click();
but seem to be something going wrong ..please any one guide me for these.
Try below code
WebElelment dropDown = driver.findElement(By.id("Fact-Communities-LIVE"));
new Select(dropDown).selectByVisibleText("SHELL SUPPLIER QUALIFICATION SYSTEM");
or
new Select(dropDown).selectByValue()("TxnyD.Communities.15.1");
Use the Select class:
Select select = new Select(driver.findElement(By.Id("Fact-Communities-LIVE");
and then use the option you want to select the options, by value, by text, by index etc.
Also check all the tags on the html if all of them are properly open and closed

Categories