jquery - Combine onLoad and Change functions? -


is there way simplify , combine these 2 functions? i'm doing large number of elements , it's becoming far cumbersome.

i've uploaded working example of code here: http://jsfiddle.net/jfjsa/5/

$(document).ready(function () {     var goalval = $("[name=objective]").val();     $("#goal_minimize, #goal_maximize, #altscenario").hide();     if (goalval === "minimize") {         $("#goal_minimize").show();     } else if (goalval == "maximize") {         $("#goal_maximize").show()     } else if (goalval == "altscenario") {         $("#altscenario").show()     }; }); $("[name=objective]").change(function () {     var goalval = $("[name=objective]").val();     $("#goal_minimize, #goal_maximize, #altscenario").hide();     if (goalval == "minimize") {         $("#goal_minimize").show();     } else if (goalval == "maximize") {         $("#goal_maximize").show()     } else if (goalval == "altscenario") {         $("#altscenario").show()     }; }); 

yep, replace anonymous functions normal one:

var dothething = function() {     var goalval = $("[name=objective]").val();     $("#goal_minimize, #goal_maximize, #altscenario").hide();     if (goalval === "minimize") {         $("#goal_minimize").show();     } else if (goalval == "maximize") {         $("#goal_maximize").show()     } else if (goalval == "altscenario") {         $("#altscenario").show()     }; };  $(document).ready(dothething); $("[name=objective]").change(dothething); 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -