1

i have a easy problem, i need do a action .click() if is clicked on a div in my array...

if($('#container_1').click(function(){ 
    $('div#image').fadeOut('fast', function(){
        $('div#cat_1').css('background-color', '#F30');
        $('#new_frame').show('fast').load('history.html');
    });
}));

if($('#container_2').click(function(){ 
    $('div#image').fadeOut('fast', function(){
        $('div#cat_2').css('background-color', '#F30');
        $('#new_frame').show('fast').load('history.html');
    });
}));

if($('#container_3').click(function(){ 
    $('div#image').fadeOut('fast', function(){
        $('div#cat_3').css('background-color', '#F30');
        $('#new_frame').show('fast').load('history.html');
    });
}));

if($('#container_4').click(function(){ 
    $('div#image').fadeOut('fast', function(){
        $('div#cat_4').css('background-color', '#F30');
        $('#new_frame').show('fast').load('history.html');
    });
}));

if($('#container_5').click(function(){ 
    $('div#image').fadeOut('fast', function(){
        $('div#cat_5').css('background-color', '#F30');
        $('#new_frame').show('fast').load('history.html');
    });
}));

Okay that is my code what works...

But i think that i can to do so much shorter... with a code like this::

Contenedores = ['1', '2', '3', '4', '5'];

if($('#container_'+Contenedores).click(function(){ 
    $('div#image').fadeOut('fast', function(){
        $('div#cat_'+Contenedores).css('background-color', '#F30');
        $('#new_frame').show('fast').load('history.html');
    });
}));

But i don't know how do it right to work...

Thanks, and sorry my poor english.

4
  • 3
    It would be much better to use classes and DOM navigation. Commented Dec 17, 2012 at 19:54
  • What does the html markup look like? Commented Dec 17, 2012 at 19:55
  • If you add a class to those elements you wouldn't need to worry about setting up the check for each id. Commented Dec 17, 2012 at 19:56
  • 1
    Is there a reason you need the if parts or am I missing something? Commented Dec 17, 2012 at 19:57

3 Answers 3

4

I'd just use the number in the ID of the clicked element to get the "cat" element. Why you would do the exact same thing and load the same content on every click is beyond me, but something like this :

$('[id^="container_"]').on('click', function(){ 
    var self=this;
    $('div#image').fadeOut('fast', function(){
        $('div#cat_'+self.id.replace('container_','')).css('background-color', '#F30');
        $('#new_frame').show('fast').load('history.html');
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

you're missing a ")" after this.id
Works, but not add the class to cat_(x) -> x = clicked container_(x)
This option is heavier than just grabbing elements by id.
Roland - Add what class exactly? @Christophe - If you think a simple string replace is heavier than looking up array values, you'll be disappointed.
@adeneo I am talking about $('[id^="container_"]') which scans the whole document. See my answer for a faster alternative.
2

There are many ways to perform this action, you can have the values comma seperated too:

$('#container_1,#container_2,#container_3,#container_4,...').click(function(){ ...

Or you can use a class name on these divs to make it much simpler:

$('.toclick').click(function(){

});

Using a class name would be my favorite.

PS: You don't need the if statement.

Comments

0

You just need to loop through your array:

var Contenedores = ['1', '2', '3', '4', '5'];

$.each(Contenedores,function(index,value){
$('#container_'+value).click(function(){ 
$('div#image').fadeOut('fast', function(){
    $('div#cat_'+value).css('background-color', '#F30');
    $('#new_frame').show('fast').load('history.html');
});
});
});

That said, there must be a better way - either classes or event delegation - but it's hard to tell without understanding your use case.

4 Comments

@Xenon - If you want to share your full working code, it is better to post it as a separate answer (rather than modifying this one). You can still accept Christophe's response as the answer. Just click the green check mark next to his answer.
@Leigh I try to do a separated answer, but i cannot answer because i have less than 10 points of rep.
@Xenon then maybe let me know as a comment what I am missing?
@Xenon - Okay, must be a restriction on answering your own question :/ FWIW, you are almost there .. just 1 more point.

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.