Passing a parameter to a function in JQuery

Posted on 2009-02-12
Last Modified: 2012-06-27
I have the following code. This is a function for a select-menu, which has several options (id: select1, select2, etc). I could copy the function for each select option, but I prefer to pass a parameter to it. I'm not really familiar with JQuery, so I wonder how to do this.

		$("#select1").change( function(){

		var selected = $("#select1 option:selected");		

		if(selected.val() == 0){ $("#warning").show(); }

		else { $("#warning").hide(); }



Open in new window

Question by:R7AF
    LVL 11

    Accepted Solution

                    $("select").change( function(){ // this will select all SELECTs on page                
                    if(this.value == 0){ $("#warning").show(); } // compare actual object's selected value 
                    else { $("#warning").hide(); }

    Open in new window

    LVL 13

    Author Closing Comment

    Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 for Beginning Beginners

    Are you looking for an efficient introduction to HTML5? Have you tried other HTML5 courses and found them too complex? Do you need just the basics of HTML5 to use with Wordpress or other content management system? If you said yes to any of these questions, this course is for you.

    Introduction HyperText Transfer Protocol ( or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
    Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question ( on how to make a page show some balloons animate up a page…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now