tags:

views:

285

answers:

1

Hello Experts,

How to use onClick() or onSelect() on option tag .Below is my code in which i want to use Onclick() or OnSelect() in option tag.

Note: where "{listCustomer}" domain object list getting in jsp page.

<td align="right"> 
  <select name="singleSelect" "> 
     <c:forEach var="Customer" items="${listCustomer}" >
     <option value="" onClick="javascript:onSelect(this);> <c:out value="${Customer}" /></option>
                </c:forEach>
          </select>         
        </td>   

Above code is not working fine please modify it.

+1  A: 

Neither the onSelect() nor onClick() events are supported by the <option> tag. The former refers to selecting text (i.e. by clicking + dragging across a text field) so can only be used with the <text> and <textarea> tags. The onClick() event can be used with <select> tags - however, you probably are looking for functionality where it would be best to use the onChange() event, not onClick().

Furthermore, by the look of your <c:...> tags, you are also trying to use JSP syntax in a plain HTML document. That's just... incorrect.

In response to your comment to this answer - I can barely understand it. However, it sounds like what you want to do is get the value of the <option> tag that the user has just selected whenever they select one. In that case, you want to have something like:

<html>
 <head>
  <script type="text/javascript">

   function changeFunc() {
    var selectBox = document.getElementById("selectBox");
    var selectedValue = selectBox.options[selectBox.selectedIndex].value;
    alert(selectedValue);
   }

  </script>
 </head>
 <body>
  <select id="selectBox" onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
  </select>
 </body>
</html>
Stephen
@Stephen.. using the above code i am creating drop down menu in which customer names are displaying. if i select anyone of the value in drop down menu that value should pass to java script..Please help me how to do it..
Manu
@Stephen.. i am having more than 1000 customer name ..in that case how can i create option#1,option#2. and also data is dynamic
Manu
In which case, you probably want to populate it server side. Do you use PHP on the server side, or another language?
Stephen
@Stephen.. my requirement is i have drop down list menu(displaying in jsp page) which contains n number of customer names. if i click anyone name in that list that selected name should send to javascript function.Please provide solution for this..
Manu
Ah, so you're working in Java Server Pages (JSP). Sorry, but that's out of my experience range. I've only used JSP once, and that was for generating just an XML file, not proper HTML. I suggest you google for some tutorials on JSP. Sorry.
Stephen
i am creating Lender/Borrower Application, in which as apart of my requirement i have to show all customer names for that Lender in drop down list of jsp page. In that drop down list if i click anyone customer name that name should pass to javascript ..after passing that selected name to javascript i am doing some manipulations..
Manu
**I do not know JSP.** I would suggest, as I said, that you go read some JSP tutorials - it seems like you're fairly unsure how to do something that may be quite simple. Apart from that, your question is now **too vague** for SO - we can answer specific programming questions, but what you are asking now is basically "Please can someone write the code to do this for me."
Stephen
The way you're generating the options is fine. What you should do for event handling is to attach a "change" handler to the "select" element, as @Stephen said.
Pointy