StudioTime
StudioTime

Reputation: 23989

Can i consolidate function calls in JQuery?

I have following and would like to be able to use one function only preferably...

$(document).ready(function(){

$("#home_p1").editInPlace({
        url: './include_eip_77994477/eip.php',
    });

$("#home_p2").editInPlace({
        url: './include_eip_77994477/eip.php',
    });

$("#home_p3").editInPlace({
        url: './include_eip_77994477/eip.php',
    });
});

This could go to 15 or 20 instances on some pages...is it possible?

Upvotes: 2

Views: 104

Answers (3)

will
will

Reputation: 4575

You could add a class to your #home_px elements and target it using the class? The urls are all the same in your example, but if they were different you could use data.

$(".element").editInPlace({
    url: $(this).data('url'),
});

<div class="element" data-url="./include_eip_77994477/eip.php"></div>

Hope that helps :)

Upvotes: 1

Manse
Manse

Reputation: 38147

2/3 options ...

1) combine the selectors :

$("#home_p1,#home_p1").editInPlace({
    url: './include_eip_77994477/eip.php',
});

2) use a class :

$(".yourclass").editInPlace({
    url: './include_eip_77994477/eip.php',
});

3) If your IDs match a pattern as in your example :

$("[id^=home_]").editInPlace({
    url: './include_eip_77994477/eip.php',
});

Upvotes: 3

Didier Ghys
Didier Ghys

Reputation: 30666

You can combine the different elements with a comma:

$("#home_p1, #home_p2, #home_p3").editInPlace({
    url: './include_eip_77994477/eip.php',
});

Or you can add a class to all your elements and use a class selector:

$(".someClass").editInPlace({
    url: './include_eip_77994477/eip.php',
});

jQuery selectors

Upvotes: 6

Related Questions