Click here to Skip to main content
15,912,021 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a drop-down menu and I wanted to do onClick onclick action for every selected item.
It seems this is not possible and I found that the code use onChange so I add my action to it.

_doBanMe('<%=strName%>', <%=strUid%>,'xxxxxxxxxxxx');

but now what I want to do is instead of xxxxxxxxxxxx I want to send the selected item value to _doBanM function

My Code
<td align="center"> 
<select style="font-size:9pt"  önChange="JavaScript:CtrlPan(this.options[this.selectedIndex].value);this.selectedIndex=0;_doBanMe('<%=strName%>', <%=strUid%>,'xxxxxxxxxxxx');">
<option value=0>test</option>                                                                                         
<option value=1>test1</option>
<option value=3>test2</option>
<option value=4>test3</option>
</select></td>
Posted
Updated 4-Mar-11 13:14pm
v3
Comments
Sergey Alexandrovich Kryukov 4-Mar-11 18:59pm    
Not clear. You should explain what do you expect from the remaining parameter(s), and what's you problems.
--SA
akoyoon 4-Mar-11 19:07pm    
when i press on any item one of the things the onchange function do is to
execute function _doBanMe('<%=strName%>', <%=strUid%>,'xxxxxxxxxxxx');
i need to send to function doBanMe instead of the value xxxxxxxxxxxx the selected item value
for example when i press on item test3
i need the onchange execute _doBanMe('<%=strName%>', <%=strUid%>,'4');
when press item test2
i need the onchange execute _doBanMe('<%=strName%>', <%=strUid%>,'3');
Dalek Dave 4-Mar-11 19:15pm    
Edited for Readability and Code Block.

1 solution

Let me give you simplified code: you just need the value of the select element, everything else you can decide on your own, using the idea:

HTML
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript"><!--
            function eventHandler(event) {
                alert(event.type);
                alert(event.value);
            }
        --></script>
    </head>
<body>

<select style="font-size:12pt" onChange="event.value = value; eventHandler(event)">
    <option value="0">test 0</option>
    <option value="1">test 1</option>
    <option value="2">test 2</option>
    <option value="3">test 3</option>
</select>

</body>
</html>


You can use several parameters, of course; and you do not have to use event object (that is a bonus :-)).

Same story with the event onКeyPress. The event object is more useful here: you can use event.keyCode and event.which. If you return false from onКeyPress event handler, a key press will be suppressed, which is very useful to filter out (ignore) the unwanted key presses.

[EDIT]
Answering follow-up question:

This is the simplified variant based on what you tried to do in your code. Don't say "I don't know how to use it"; this is the same as you tried to write, only simple and correct. All you need to do is replacing alert with the code you wanted. You wanted to get a selected value, that's it. A parameter will be taken from selected value, that is, 0, 1, and so on.

HTML
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript"><!--
            function eventHandler(value) {
                alert(value);
            }
        --></script>
    </head>
<body>

<select style="font-size:12pt" onChange="eventHandler(value)">
    <option value="0">test 0</option>
    <option value="1">test 1</option>
    <option value="2">test 2</option>
    <option value="3">test 3</option>
</select>
</body>
</html>


—SA
 
Share this answer
 
v7
Comments
akoyoon 4-Mar-11 19:23pm    
i am not really sure how to use this code with my onChange

onChange="JavaScript:CtrlPan(this.options[this.selectedIndex].value);this.selectedIndex=0;_doBanMe('<%=strName%>', <%=strUid%>,'xxxxxxxxxxxx');">
Sergey Alexandrovich Kryukov 4-Mar-11 20:55pm    
You stay with your xxx... Ok, just write "value" instead of this; it will give you 0, 1, 2, 3 or 4. Is that clear?
--SA
akoyoon 4-Mar-11 20:30pm    
thnx for your help but i found a very easy way to do what i want
Sergey Alexandrovich Kryukov 4-Mar-11 20:54pm    
Than share it!
Mind you, I don't believe it can be simpler, so I'm curious.
Also, the fact you didn't understand my sample tells me something's wrong... :-)
--SA
akoyoon 4-Mar-11 21:10pm    
you are correct my way didn't work at all i thought it can be done like this
<td align="center">
<select style="font-size:9pt" önChange="JavaScript:CtrlPan(this.options[this.selectedIndex].value);this.selectedIndex=0;">
<option value=0 önChange="_doBanMe('<%=strName%>', <%=strUid%>,'0');">test</option>
<option value=1 önChange="_doBanMe('<%=strName%>', <%=strUid%>,'1');">test1</option>
<option value=3 önChange="_doBanMe('<%=strName%>', <%=strUid%>,'3');">test2</option>
<option value=4 önChange="_doBanMe('<%=strName%>', <%=strUid%>,'4');">test3</option>
</select></td>

i am not a programmer and i donot understand how your way work or what i am suppose to do can you help me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900