Dynamically create dropdownlist on click button in JSP - java

I am new to JSP for 1 month.I wants to dynamically create dropdownlist whenever user click "Add" button in JSP. But, after searching for hours in the internet, there are no such articles. Previously I had tried to do the same thing (but with textbox in C#) and it works. Is it possible to dynamically create dropdownlist in JSP too? Or I have to send to servlet to create another dropdownlist?
<form>
<select>
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
<input type="Add" name="Add">
<form>

You could either use javascript to add element to select item or you can submit form and render new list with element you added after reload

Related

Selenium is not selecting the correct value

Im using selenium to test a web page which has a select option and one input type text.
Manually and using the selenium IDE, it works correctly, but when I export the test case to Java Junit, i can see the dropdown click, but selenium is not selecting the value, it is just expanding the dropdown.
What can i do?
Lets check my code:
Select dropdown = new Select(driver.findElement(By.id("type")));
dropdown.selectByVisibleText("Celcius");
Consider my form, like:
<form action="doit">
<select name="type" id = "type">
<option value = "fail"> Fail </option>
<option value = "celcius"> Celcius </option>
</select>
<input type="number" name="num" id="num">
</form>
Sometimes this method wont work. This may happen if the text between the option tags have spaces before and after the tags.
Eg.
1.<option> Fail </option>
2.<option>Fail</option>
In the above example 1 and 2 are different.
So you can use like,
driver.findElement(By.id("type")).click(); // this will expand the list
driver.findElement(By.xpath("//select[#id='type']/option[contains(text(),'Celcius')]")).click();
Also try to click directly. It work if the element is vivible,
driver.findElement(By.xpath("//select[#id='type']/option[contains(text(),'Celcius')]")).click();
driver.findElement(By.id("type")).sendkeys("Celcius");

how to get value of select in jsp which is defined on same jsp page

I have defined a select(drop down) in my jsp page. When user will select one of these options i want to display some text boxes. I will use jsp tags to display those textboxes. But I don't know how can i get value of select tag in jsp on the same page. Here is my code:
<div class="col2" >
<select class="textbox" id="noo" name="noo" style="margin-top:10px;" required>
<option value="">Select</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</div>
<%
int noo=
%>
What should i write after = to get the selected value in variable?
This is possible if you use JSTL. You would need to add the jstl.jar file to your lib folder but then the selected value in the variable would be obtained using
${param.name}
JSP pages compile at server side not client side. So,its not possible that you select an option value and take action on it.
Yes, this similar thing you can do by JQuery or JavaScript.
If you really want to take support of server side code than ajax can help you.
Send selected value to servlet by ajax set response in session or request and iterate session or request in jsp .
JSP is a server side language. Whole html code will be generated before it sends a response to web browser.
So If you want to change something with respect to the value selected by user,
you must be using a client side scripting language like javascript.
Or submit value to server and generate a new page with respect to based on value submiteted.
There are two options:
To submit the form and get the attributes in another jsp or servlets and then return the value using the request.setAttribute(). After which you can get the attributes in the jsp part to display
You can use jquery that is get the value of the select tag after an event occurs e.g after selection e.g
var data = $('#noo').find(":selected").text();
Then display the data using a class or id selector.e.g
$('p').val(data)

how to pass dropdown selected value from one jsp to another in springs

I have two jsp pages in my spring project. In first jsp I have one dynamic dropdown list and one link tag which directs to second jsp page. I want to pass the selected item of dropdown list to second jsp. So how can I pass parameter as selected value in href and how can I get the value in second jsp.
<form:select id="Id" path="Id">
<form:options items="${IdList}">
</form:select>
Second Page
Because u want to pass it via the a tag, href attribute, add a onchange handler on your select tag, then at that handler modify the href attribute of the second page link. You should use javascript.

how to add values into dropdown in java script

I saw a sample code in this website to dynamically populate second dropdown men based on a selection of first dropdown. I have account number dropdown and based on the selection, I need to populate second dropdown with corresponding email-id.
code is here :Populating Child Dropdown (second option using java script).
But can someone let me know how to populate the values inthe second dropdown ?
<script>
var dd2options = ${dd2optionsAsJSObject};
var dd3options = ${dd3optionsAsJSObject};
function dd1change(dd1) {
// Fill dd2 options based on selected dd1 value.
var selected = dd1.options[dd1.selectedIndex].value; ... }
</script>
My code is as below- how to change the code to do this ?
<td>
1. Member Account Number
<span class="bodyCopy">
<font color="#ff0000"> * </font>
</span>:
<html:select
name="DataForm"
property="Member.accountNumber"
styleClass="formContent"
style="width:80px">
<html:options collection="<%= WorkConstants.RENewDropdowns.PACCT %>"
property="value"
labelProperty="label"
styleClass="formContent"/>
</html:select>
</td>
My second dropdown is as below:
<td>
3. Member <br>E-mail Address:<br />
<span class="bodyCopy"></span>
<html:select
name="DataForm"
property="Member.emailAddress.emailAddress"
style = "width:150px"
styleClass="formContent">
<html:options collection="<%= WorkConstants.RENewDropdowns.PEMAIL %>"
property="value"
labelProperty="label"
styleClass="formContent"/>
</html:select>
</td>
Appreciate your help on this as I'm new to java script.
Problem can be solved in following ways
Using onchange event of select element, on first box submit form and populate data in second select box.
Use ajax to fetch data in the same way instead of submitting form
Fetch all the main and related data, store them in arrays. Call them according to key. You can use JSON to solve

Tapestry 4, get submitted value from non-component element

My form has a custom element like below, created using custom ajax:
<select jwcid="testtest <at> Any">
<option value="x">California -- CA</option>
<option value="y">Colorado -- CO</option>
<option value="z">Connecticut -- CN</option>
</select>
After the form is submitted, how do I get the value of this custom html element?
cycle.getPage().getComponents().get("testtest") ?
If I understand you correctly, you have a form element generated not by Tapestry, but by something else.
First of all, jwcid has no place in your HTML code, it's only used in Tapestry component templates. Second, the select element must have a name attribute or else your browser won't submit it at all:
<select name="name-of-element">
...
</select>
To get the submitted value on the server side, use cycle.getParameter("name-of-element") in your page/component class.

Categories