0

I've had this javascript function that I've used many many times over ...

function showHideObjects()
{
    var args;
    args = showHideObjects.arguments;

    for(var i=0;i<args.length;i++)
    {
        if(document.getElementById(args[i]))
        {
            if(args[i+1] == 'flip')
            {
                if(document.getElementById(args[i]).style.display == '')
                {   document.getElementById(args[i]).style.display = 'none';}
                else
                {   document.getElementById(args[i]).style.display = '';}
            }
            else
            {       
                document.getElementById(args[i]).style.display = args[i+1];
            }
        }
        i++;
    }

}

Now I'm working with ASP.NET and need that same function but in jQuery but I can't find any information about dynamic parameters in jQuery. Is there a way to do this in jQuery?

To provide a little more background ... you could call the above code with a line like ... showHideObjects('div1','none') and it'd hide div1. Or you could call ... showHideObjects('div1','none','div2','','div3','flip') and it'd hide div1, show div2 and switch div3 from either hidden or shown.

3
  • Can you give a "fake" example of how you'd like to use this in jQuery? In other words, just make up some jQuery-ish syntax for illustrative purposes. As it currently stands, you question isn't that clear. In fact, there are plenty of places where you CAN use multiple parameters in jQuery methods... in fact, in just about all of them: $('div').css({height:'10px',width:'30px',position:'absolute'...}); Commented Aug 1, 2011 at 19:22
  • 1
    jQuery is built using Javascript. The same thing would work in jQuery also as jQuery doesn't alter the language semantics. Commented Aug 1, 2011 at 19:23
  • jQuery is not a language. It is a library for JavaScript. Commented Aug 1, 2011 at 19:28

5 Answers 5

4

jQuery is just JavaScript. Your code will work fine with jQuery.

Sign up to request clarification or add additional context in comments.

Comments

2

It is purely a javascript feature. Jquery do not have any special feature for this.

Comments

2

As some have already said, jQuery is Javascript and your code will work just fine. BUT you can do the same thing with jQuery that you are now doing with your function. You are getting elements which you change to have either display none or nothing.

jQuery has Selectors, which you can use to select the elements from the DOM to which you want to do something to. You can forexample use selectors to select the element you want and then just cast .toggle(). It does the exactly same thing as your function but with just 1 line of code.

EDIT: Added an example:

If you have a div, which has a class hideNseek, you can use this to toggle it to show or hide:

$(".hideNseek").toggle();

$(".hideNseek") is selectors which selects all classes that have that name.

Comments

1

Like others have said, your function will work just fine. You don't NEED jQuery to do what you are doing. If you wanted to use jQuery you could do something like:

function showHideObjects() {
    var args = arguments,
        l=args.length;

    for ( var i=0; i<l; i++ ) {
        var elem = $( "#"+args[i] ),
            type = args[i+1];

        if ( elem.length ) {
            if ( type == "flip" ) {
                elem.toggle();
            } else {
                elem.css("display", type);
            }
        }
        i++
    }
}

Comments

0

You can't call different actions on a group in JQuery. However, you can perform the same action (hide, show, toggle) on a group of elements by using the appropriate selector to get the group. Here's a quick example that has buttons to toggle all the div elements and all the span elements on the page with the JQuery toggle function, along with a fiddle for testing:

HTML:

<div>div 1</div>
<span>span 1</span>
<div>div 2</div>
<span>span 1</span>

<input id="toggleSpans" type="button" value="toggle spans"/>
<input id="toggleDivs" type="button" value="toggle divs"/>

JavaScript:

$('#toggleDivs').click(function(){
    $('div').toggle();
});
$('#toggleSpans').click(function(){
    $('span').toggle();
});

If that is not good enough, you can easily group and call these methods in a custom function as you do in your old JavaScript function, or just keep your old method.

UPDATE:

Check out the different selectors you can use. In your specific case, you'd need to use ID Selector along with Multiple Selector, but you could make your function much more powerful if you allowed passing in any JQuery selector rather than just the ids. For example:

showHideObjects('#div1, <otherSelectorsToHide>','none',
    '#div2, <otherSelectorsToShow>','',
    '#div3, <otherSelectorsToToggle>','flip');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.