jsp fill in field on dropdown item changed - java

I'm trying to make it so that when an option from the combobox is selected it fills out some of the later input fields. current code (below) doesnt give anny errors just simply does not give output on item changed.
my question being how can I make it so that when selected item changes it fills in some fields.
<div>
options:<select id="optionbox" onchange="Change()">
<option value="op1">option1</option>
<option value="op2">option2</option></select><br>
<form action="KlusServlet.do" method="post"> //not relevant i think used for servlets later on
<input id="description" type="text"></input>
</form>
<script type="text/javascript">
function Change() {
var e = document.getElementById("optionbox");
var selOption = e.options[e.selectedIndex].value;
document.getElementById("description").innerHTML = "selected: " + selOption;
}
</script>
</div>

for textboxes you need to use .value not innerHTML like below
document.getElementById("description").value = "selected: " + selOption;

you can use jquery code $('#selector').val(value); for that
Working Code
<div>
options:<select id="optionbox" onchange="Change()">
<option value="op1">option1</option>
<option value="op2">option2</option></select><br>
<form action="KlusServlet.do" method="post"> //not relevant i think used for servlets later on
<input id="description" type="text"></input>
</form>
<script type="text/javascript">
function Change() {
var e = document.getElementById("optionbox");
var selOption = document.getElementById("optionbox").value;
alert(selOption);
$('#description').val(selOption);
}
</script>
</div>

Related

How to send dropdown value to servlet NOT the option text

I have dropdown with options and the values. I can get the option value by the dropdown name in servlet but how can i get the dropdown "value" in servlet. In screenshot, temporarily i concatenated the with options but i want to store value in variable in servlet.
Please help:
HTML:
<input type="text" name="taxiDropdown" id= "taxiDropdown" placeholder="Search taxi...">
</div>
<div class="scrolling menu">
<%
List eList = (ArrayList) session.getAttribute("taxiInfo");
%>
<%
for (int i = 0; i < eList.size(); i++) {
%>
<div class="item" data-value="<%=((TaxiInfo) eList.get(i)).getID()%>">
<div class="ui green empty circular label"></div>
<%=((TaxiInfo) eList.get(i)).getTaxiPlate() +" "+ ((TaxiInfo) eList.get(i)).getID() %>
</div>
<%
}
%>
</div>
</div>
Servlet:
String val = request.getParameter("taxiDropdown");
(in "val", I want to store the value of the dropdown not the option text)
In JSP you should have something like that:
<form method="post">
<select name="taxiDropdown" id="taxiDropdown">
<%
List<TaxiInfo> eList = (List<TaxiInfo>) request.getAttribute("taxiInfo");
for (TaxiInfo taxiInfo : eList) {
%>
<option name="<%=taxiInfo.getTaxiPlate()%>" value="<%=taxiInfo.getID()%>"><%=taxiInfo.getTaxiPlate()%></option>
<%
}
%>
</select>
<input type="submit" />
</form>
Then in controller/servlet you will receive the id of TaxiInfo:
String val = request.getParameter("taxiDropdown");
System.out.println(val);
Or in your case you should set a hidden input with javascript with desired value.
added this code in html:
Move value of the dropdown selection to hidden textbox
<script type='text/javascript'>
$(function() {
$('#driverdp').change(function() { <-- this is my dropdown -->
var x = $(this).val();
$('#driverid').val(x); <-- this is my textbox -->
});
});
</script>
Servlet:
get the value of hidden text in servlet
String text= request.getParameter("driverId");
hope it will help someone

Spring, Thyme: redirecting to <select> value

I have a small problem, I just need to redirect to the value of select like:
/results/name/Heli Redel
How would I go about doing that?
<div class="search">
<select th:required="true">
<option th:value="'Eha Raudreha'" th:text="'Eha Raudreha'"></option>
<option th:value="'Heli Redel'" th:text="'Heli Redel'"></option>
<option th:value="'Mait Kuusevaik'" th:text="'Mait Kuusevaik'"></option>
</select>
<a class="buttons" th:href="#{|/results/name/NAME_HERE|}"><button class="deleteBtn">Otsi</button></a><br />
</div>
Add a jQuery function, that when the button is clicked, it redirects to a another url.
jQuery(document).ready(function() {
$('.deleteBtn').on('click', function() {
var selectValue = $('.search').find('select').val()
window.location.replace("/result/name/" + selectValue);
})
})
I would add some ids to your elements to make it easier to fetch data.

Fetching only selected values from CrossSelect of Jquery

In below screen shot there are 2 boxes where user will select from left box to right which ever items he want. Now which ever the column selected by the user in the right box. I need only those items..
Below is the screen shot and code.In the below screen shot
i need to get only one,two and three values from right box.So, how to get only selected items from the right box?
To Fetch the right box values is this the right menthod in JavaScript Function. Please help
function export()
{
<%if(request.getParameter("myServersID1") != null)
{
String[] ItemNames;
ItemNames = request.getParameterValues("myServersID1");
for(int i = 0; i < ItemNames.length; i++)
{
String selecteditems;
selecteditems= ItemNames[i];
System.out.println("selecteditems" +selecteditems);
}}%>
alert("selectcol");
}
when button is added
<input type="button" value="Export" class="btn-arrow-pri btn-small" style="margin-left: 300px;" onclick="export();"/>
___ ____
four | |
|> |one
five |< |two
| >> |three
|<< |
____
CODE is:
<html>
[...]
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function moveRight(){
$(document).ready(function(){
$("#myserversID option:selected").appendTo("#myserversID1")
});
}
function moveLeft(){
$(document).ready(function(){
$("#myserversID1 option:selected").appendTo("#myserversID")
});
}
</script>
</head>
<body>
<select size="8" id="myserversID" multiple="multiple">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
<option value="five">five</option>
<option value="six">six</option>
</select>
<input type="button" value="Move Right" onclick="moveRight();"/>
<input type="button" value="Move Left" onclick="moveLeft();"/>
<select size="8" id="myserversID1" multiple="multiple">
</select>
</body>
</html>

display selected value of the drop down in jsp page

I want to display selected value. In the text field I can display it within value like below
value ="<%=event_data.getE_venue()%>"
code :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" value ="<%=event_data.getE_target_category()%>" id="single1">
<option>Sports</option>
<option>Corporate</option>
<option>Religious</option>
<option>Music</option>
</select>
but in dropdown box it doesn't work.
please help me. thanks..
Firstly, select doesn't work in that way , you need to put selected attribute in option that matches your input.
for example:
<option selected='selected'>Sports</option>
check this fiddle :
http://jsfiddle.net/ZLTS7/
your code should be something like :
<input type="text" name="where" placeholder="Add a place" size="23" value ="<%=event_data.getE_venue()%>"/>
<select name="category" id="single1">
<option <%= (event_data.getE_target_category().equals("Sports")?"selected='selected'":"") %>>Sports</option>
<option <%= (event_data.getE_target_category().equals("Corporate")?"selected='selected'":"") %>>Corporate</option>
<option <%= (event_data.getE_target_category().equals("Religious")?"selected='selected'","") %>>Religious</option>
<option <%= (event_data.getE_target_category().equals("Music")?"selected='selected'":"") %>>Music</option>
</select>
You need to change the dropdown value through Javascript or jQuery. You can assign desired value to dropdown just like input
var dd = document.getElementById('single1');
var opts = ddl.options.length;
var value = <%=event_data.getE_venue()%>;
for (var i=0; i<opts; i++){
if (dd.options[i].value == value){
dd.options[i].selected = true;
break;
}
}
or if you are using jQuery.
$("#single1").val(value);
See this example:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#slectboxid option').click(function(){
$('#textboxid').val($(this).val());
});
});
</script>
</head>
<body>
<form action="#" method="post">
<select name="select" id="slectboxid">
<option value="test">test</option>
<option value="test2">test2</option>
</select>
<input type="text" name="text" id="textboxid" />
</form>
</body>
</html>

How to run a child window java script function in parent window after changing the url in the parent window?

I want to execute the java script function of a child window on parent window, Even after changing the URL in the parent window.
I tried like below:
**Parent.html:**
<html>
<head>
<title>Parent window document</title>
</head>
<body>
<script type="text/Javascript">
function openChildWindow()
{
var s_url = "child.html";
var s_name = "ChildWindowDocument";
var s_specs = "resizable=yes,scrollbars=yes,toolbar=0,status=0";
var childWnd = window.open(s_url, s_name, s_specs);
var div = childWnd.document.getElementById("child_wnd_doc_div_id");
div.innerHTML = "Hello from parent wnd dddsfds";
}
</script>
<input type="button" value="Open child window document" name="sss" onclick="openChildWindow()" />
<div>Click me: nothing will happen.</div>
<div id="aaa" class="yourclass">
<p>This is a paragraph with a <span>span</span> where the paragraph's container has the class. Click the span or the paragraph and see what happens.</p>
This sentence is directly in the div with the class rather than in child element.
</div>
<div>Nothing for this div.</div>
<a>dsfsd</a>
</body>
</html>
**child.html:**
<html>
<head>
<title>Parent window document</title>
<script>
opener.document.body.onclick = function(e) {
var sender = (e && e.target) || (window.parent.event && window.parent.event.srcElement);
document.getElementById("id").value=sender.tagName;
}
</script>
</head>
<body>
<div id="child_wnd_doc_div_id">child window</div>
ID: <input type="text" id="id"/><br/>
Name: <input type="text" id="id"/><br/>
Xpath: <input type="text" id="id"/><br/>
CSS: <input type="text" id="id"/><br/>
</body>
</html>
Clearly, my concept is: I want to execute the child window function after changing the url in the parent window.
The only way I see:
observe the unload-event of the opener. When it fires, try(after a short timeout) to reassign the onclick-function(and also the onunlad-function).
Of course this will only work when both documents are placed under the same domain.

Categories

Resources