0

I have developed some code for image sliding in which whenever a mouse event occurs I want the id of the other div except on which the mouse is present.

My code is:

$('#div1,#div2,#div3,#div4,#div5,#div6').mouseover(function () {
    $('#div1,#div2,#div3,#div4,#div5,#div6').css('width', '100px')
    $("." + $(this).data('class')).animate({                   
        'width':"400px",                   
    }, {
        'duration':1500,
        easing:'easeOutBack',
    })
});
6
  • 6
    Ohhh dear, use class instead 100K of ids. Commented Feb 5, 2013 at 9:53
  • 1
    Try jquery.not(this): api.jquery.com/not Commented Feb 5, 2013 at 9:54
  • 1
    Can you provide an example of your problem. It's not very clear in your question. Also, a description of why the code you have doesn't work would be helpful. Commented Feb 5, 2013 at 9:54
  • Try $('#'+this.id).animate({yourcode}); Commented Feb 5, 2013 at 9:56
  • @Dineshkani Why on earth would you do that? this is already the element you want, re-selecting it using its id property makes no sense. Commented Feb 5, 2013 at 10:02

3 Answers 3

1

I would suggest altering this to use classes rather than ids. It would make your code much neater.

Give every element that you want to have this effect a class of animated and try this code instead.

$('.animated').mouseover(function () {
  $(this).removeClass('animated');
  $('.animated').css('width', '100px')
  $(this)
    .addClass('animated')
    .animate({                   
      'width':"400px",                   
    }, 1500);
});

Here is an example showing it in action. Although I don't think your code does what you want it to.

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

Comments

0

Use .not to remove the hovered over div from the set of matched elements.

$('#div1,#div2,#div3,#div4,#div5,#div6').mouseover(function () {
    $('#div1,#div2,#div3,#div4,#div5,#div6').not(this).css('width', '100px')
    $("." + $(this).data('class')).animate({                   
                    'width':"400px",                   
                }, {
                    'duration':1500,
                    easing:'easeOutBack',
                })
    });

On a side note, it would be cleaner to use a class selector in this situation.

Comments

0
$( function() {
$("[id^=div]").mouseover( function() {
  var my = $(this).attr("id");
  var id = new Array;
  $("[id^=div]").each( function() {
    if($(this).attr("id") != my) {
       id.push($(this).attr("id"));
    }
  });
});
});

Working Fiddle or Demonstration

You can access each and every property.

I have taken all ids and added all ids to array id. You can access it further in your function.

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.