This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes calling function on the basis of option value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "calling function on the basis of option value?" Watch "calling function on the basis of option value?" New topic
Author

calling function on the basis of option value?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Hi,

I have a dropdown box has some values, on my page, user can select any one value at a time, I need to call a particular javascript function on the basis of what user has selected in dropdown box.

Here is what I tried...



But it is giving error, because I am calling variable as a function...

Please suggest me...

Thanks.
[ December 26, 2005: Message edited by: rathi ji ]
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
You cannot call variable as function name...you can use if/else or switch statements which can fulfill your need.


Debashree Halder<br />SCJP 1.5<br />Washington DC
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Debashree Halder:
You cannot call variable as function name...you can use if/else or switch statements which can fulfill your need.


Yes, but as you might have noticed that function names are dependent on value of options. They are actually generated dyanamically...

Please help me...

Thanks.
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
What you can do is pass the variable as arguement to the calling function and instead of calling fun_A, fun_B etc....call like this....

fun(optionValue)

Where optionValue contains the information about which item is selected.

Hope this will solve your problem....

Regards,
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Debashree Halder:
What you can do is pass the variable as arguement to the calling function and instead of calling fun_A, fun_B etc....call like this....

fun(optionValue)

Where optionValue contains the information about which item is selected.

Hope this will solve your problem....

Regards,


Sorry, but I didn't get your point. Could you please explain it with little bit code...
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Did you mean this:



Hope you are getting my point.

Please help...

Thanks.
[ December 26, 2005: Message edited by: rathi ji ]
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
function fun1() {

var selectedIndex = document.f1.s1.selectedIndex;
var optionValue = document.f1.s1.options[selectedIndex].value;

fun(optionValue );

}

function fun(optionValue ) {
if(optionValue =="A")
alert("A selected")
else if(optionValue =="B")
alert("B selected")
else if(optionValue =="C")
alert("C selected")
...
...
...
}

Hope this would explain :-)
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
function fun1() {
var selectedIndex = document.f1.s1.selectedIndex;
var optionValue = document.f1.s1.options[selectedIndex].value;
if(optionValue =="A")
fun_A();
else if(optionValue =="B")
fun_B();
else if(optionValue =="C")
fun_C();
else if(optionValue =="D")
fun_D();
}

function fun_A() {
alert("Option A Selected")
}
function fun_B() {
alert("Option B Selected")
}
function fun_C() {
alert("Option C Selected")
}
function fun_E() {
alert("Option E Selected")
}
function fun_D() {
alert("Option D Selected")
}
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
Try any of the above two.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Debashree Halder:
Try any of the above two.


But, I don't know, how many functions will be created, it is totally based on number of options dropdown box has...

So I can't write *if-else* code myself....



Can I write this first function fun1 (that has *if-else* code) at run time with the help of values of options...

???
[ December 26, 2005: Message edited by: rathi ji ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: calling function on the basis of option value?