I have a form wherein I have to select an item from the drop down menu and display the selecetd value on the form. The values in the dropdown menu come from the database. Here is my code of select:
<aui:select id="empName" name="empName" onChange = "showEmpName()">
<%
List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);
for(Employee emp : EmpList ) {
%>
<aui:option value='<%=emp.getEmpFname()%>' name = "leaveEmp" onClick = "showEmpName()"><%=emp.getEmpFname()%> <%=emp.getEmpLname()%></aui:option>
<% } %>
Here is the script: the value selected from drop down should be displayed on the form
<script>
function showEmpName()
{
var empName = document.getElementById("empName")
var showEmpName = document.getElementById("showEmpName")
showEmpName.value = empName.value
alert("my name is" + empName)
}
</script>
I have a couple of questions regarding this: 1. onchange onclick aren't working. 2. Secondly I want to display the selected item on the form.
How should I approach?
EDIT: I even tried the follwing but it doesn't work at all.
var selectedEmpName = document.getElementById('empName');
var absentEmp = document.getElementById('absentEmp');
selectedEmpName.onchange = function() {
absentEmp.value = selectedEmpName.value;
};
</script>
<aui:select id="empName" name="empName">
<%
List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);
for(Employee emp : EmpList ) {
%>
<aui:option value='<%=emp.getEmpFname()%>' name = "leaveEmp"><%=emp.getEmpFname()%> <%=emp.getEmpLname()%></aui:option>
<% } %>
</aui:select>
The above code I am trying to display in a non editable text box. What I actually want is once the value from drop down is selected it should be displayed as the value of the radio button which already exists. once the value of this radio button is set, it will be used for further processing.
EDITCODE:
<!DOCTYPE HTML>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
$(document).ready(function(){
$('#empName').change(function(){
var value = $(this).val();
console.log(value);
$('label').text(value);
});
});
</script>
</head>
<body>
<portlet:actionURL name="markAbsent" var="markAbsentURL" />
<aui:form name="markAbsent" action="<%=markAbsentURL.toString() %>" method="post" >
<aui:select id="empName" name="empName" >
<%
List<Employee> EmpList = EmployeeLocalServiceUtil.getEmployees(-1,-1);
for(Employee emp : EmpList ) {
%>
<aui:option value='<%=emp.getEmpFname()%>'><%=emp.getEmpFname()%> <%=emp.getEmpLname()%></aui:option>
<% } %>
</aui:select>
<label for="empName" id = "labelname"></label>
</aui:form>
Say you have the following html
And you use jQuery, then you should be able to listen to selections by
Working example: http://jsbin.com/edamuh/3/edit
Or you could also do it with basic javascript, by putting the following after the generated select & label:
Working example here: http://jsbin.com/edamuh/7/edit
Note: The latter might not work equally in all browsers. Eg. Works in Firefox(Windows) but might not in others. Thus I'd suggest opting for using jQuery.
EDIT
In your case the markup should be something like (no onChange):
Working example with radio buttons: http://jsbin.com/edamuh/12/edit
It is important to notice, that when the document loads the select must have been already rendered, otherwise the script won't work.