0

I want to loop through JQUERY based on a count that I would input. Specifically if I enter this for entry 0, this code works:

$(document).ready(function()
{
    $('.0').mouseenter(function()
    {
        $('.hideme0').fadeIn('slow');
    });
    $('.0').mouseleave(function()
    {
        $('.hideme0').fadeOut('slow');
    });
});

I now wish to replace ('.0') Which is dynamic CSS class with the name 0 with a loop counter for example

for (var n = 0; n < 3; ++ n)
{
    $(**n**).mouseenter(function()
    {
        $('.hideme**n**').fadeIn('slow');
    });
    $(**n**).mouseleave(function()
    {
        $('.hideme**n**').fadeOut('slow');
    });
}

Any ideas on how to do this with jQuery? I can't find info in the API or with a Google search for instructions.

I hope this is clear, and you understand what I'm trying to do.

0

4 Answers 4

2

What don't you get? It's just a simple string so you can build it with concatenation:

$('.' + n).mouseenter(function()
{
    $('.hideme' + n).fadeIn('slow');
//...
//and so on

However, using this in a for loop will like cause an issue due to closures. Which you can solve like so:

for (var n = 0; n < 3; ++ n)
{
    (function(x){
        $('.' + x).mouseenter(function()
        {
            $('.hideme' + x).fadeIn('slow');
        });
    })(n);

    (function(x){
        $('.' + x).mouseleave(function()
        {
            $('.hideme' + x).fadeOut('slow');
        });
    })(n);
}

Here is a working example using your HTML (from comments)

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

13 Comments

Thanks for the response, I really appreciate it but this solution didn't work for me unfortunately. Thanks anyway
@StephenKennedy: What part doesn't work? Do you get any console errors? Have you tried debugging it?
Hi, I'm debugging it now musefan, its a pain! Can you include the code including the $(document).ready() element? I'm wondering now if I'm just doing something crazy. First I thought my PHP might be causing the error but its working fine if I don't loop it, if I just set them up one by one.
@StephenKennedy: Create a JSFiddle with the HTML and I will make the javascript work with it, then you can see it in action
Musefan, you are the man! I don't know why it didn't work initially for me, maybe I didn't have the $(document).ready in the right place or something..anyway kudos. Thanks, you've made my day. You probably don't need coding tips from me, but if you ever need help, give me a shout
|
2
for (var n = 0; n < 3; n++)
{
    $("." + n).mouseenter(function()
    {
        $('.hideme' + n).fadeIn('slow');
    });

    $("." + n).mouseleave(function()
    {
        $('.hideme'+ n).fadeOut('slow');
    });
}

7 Comments

should be $("." + n)
you caught it while I was editing. thanks.
Hi All, thanks very much for getting back. Still not working:for (var n = 0; n < 20; n++) { $('.' + n).mouseenter(function() { $('.hideme' + n).fadeIn('slow'); }); $('.' + n).mouseleave(function() { $('.hideme'+ n).fadeOut('slow'); }); } This is the code I'm currently using. I checked in my comments on my js page and I had actually already tried this.
does the console produce an error?
No error, the fadin and fadeout just don't seem to happen, I'm debugging now...thanks again for trying to help
|
1

You can just concatenate the string and use that as your selector:

var index = 1;
var selector = '.hideme' + index;
$(selector).fadeOut('slow');

Comments

0

It should just be a matter of concatenating the number value as part of the selector string:

$('.' + n).mouseenter(function()
{
    $('.hideme' + n).fadeIn('slow');
});
$('.' + n).mouseleave(function()
{
    $('.hideme' + n).fadeOut('slow');
});

Numerics can be interpreted as strings implicitly in JavaScript in this manner.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.