user4207523
user4207523

Reputation:

Variable's value transfer from Jsp to Javascript

I have a dropdown of roles in JSP file. When a role is selected from the dropdown I want to get the selected role ID. When a role is selected then an onchange event occured and a function 'callAjax(roleId) is triggered. Here the roleId is jsp variable. I am trying to pass it through a javascript. But I can not able to do this. Is it really possible? Can any one help me to doing this. Thanks in advance.

<select name="role" onchange="callAjax(roleId)">
 <c:forEach items="${pro.roles}" var="rol" varStatus="status">
   <option value="${rol.id}">
    <c:out value="${rol.name}"/>
  </option>
 </c:forEach>               
</select>

Upvotes: 1

Views: 98

Answers (3)

nbrooks
nbrooks

Reputation: 18233

Remove the onchange="callAjax(roleId)" attribute from your select, and bind a change handler to the element with jQuery. The value returned by val will be the value from the selected option, which corresponds to the rol.id assigned to that option.

$(function() {
    $("select[name='role']").change(function() {
        callAjax($(this).val());
    });
});

Upvotes: 1

Jai
Jai

Reputation: 74738

I guess you want to use the value which contains the id, so change to this:

 onchange="callAjax(this.value)"

Upvotes: 1

gaauspawcscwcj
gaauspawcscwcj

Reputation: 3

try this : onchange="callAjax('<%=roleId%>')"

Upvotes: 0

Related Questions