Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-27-2012, 11:10 PM   PM User | #1
UD2006
Regular Coder

 
Join Date: Jul 2007
Location: Velsen Noord, Netherlands
Posts: 206
Thanks: 6
Thanked 0 Times in 0 Posts
UD2006 is an unknown quantity at this point
make function shorter

I have the following JQuery code (working), but I would like to know if it can be shorter and if so, how?

Code:
$(function(){
  $('.blok').mouseenter(function(){
    $(this).find('#message-1').show();
  }).mouseleave(function(){
    $(this).find('#message-1').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-2').show();
  }).mouseleave(function(){
    $(this).find('#message-2').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-3').show();
  }).mouseleave(function(){
    $(this).find('#message-3').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-4').show();
  }).mouseleave(function(){
    $(this).find('#message-4').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-5').show();
  }).mouseleave(function(){
    $(this).find('#message-5').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-6').show();
  }).mouseleave(function(){
    $(this).find('#message-6').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-7').show();
  }).mouseleave(function(){
    $(this).find('#message-7').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-8').show();
  }).mouseleave(function(){
    $(this).find('#message-8').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-9').show();
  }).mouseleave(function(){
    $(this).find('#message-9').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-10').show();
  }).mouseleave(function(){
    $(this).find('#message-10').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-11').show();
  }).mouseleave(function(){
    $(this).find('#message-11').hide();
  });
  $('.blok').mouseenter(function(){
    $(this).find('#message-12').show();
  }).mouseleave(function(){
    $(this).find('#message-12').hide();
  });
});
The message-#(numbers) are div's.

Thanks
UD2006 is offline   Reply With Quote
Old 03-28-2012, 12:35 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,587
Thanks: 5
Thanked 864 Times in 841 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Hell yeah, this can be made shorter. But it would help to see your HTML in order to suggest a more generic traversal method.

The first thing that comes to my mind right now would be:
Code:
 $('.blok').mouseenter(function(){
   $(this).find('[id*=message]').show();
 }).mouseleave(function(){
   $(this).find('[id*=message]').hide();
 });
That’s the “contains” selector; alternatively you could use the “starts with” selector: [id^=message] or the “contains prefix” selector (string separated by hyphens): [id|=message]
__________________
Don’t click this link!

Last edited by VIPStephan; 03-28-2012 at 12:40 AM..
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
UD2006 (03-28-2012)
Old 03-28-2012, 12:42 AM   PM User | #3
Mishu
Banned

 
Join Date: Mar 2012
Posts: 306
Thanks: 1
Thanked 28 Times in 28 Posts
Mishu can only hope to improve
Quote:
Originally Posted by UD2006 View Post
I have the following JQuery code (working), but I would like to know if it can be shorter and if so, how?
Do you mean just shorter jquery code or shortening the total actual javascript that your jquery runs in the background?

Sometimes newbies get excited because they can do something with just 1 or 2 lines of jquery without realising that the jquery could be running 50, 100 or 100's of lines of javascript in the background to do that thing.

If you code the task using plain javascript then 99.99% of the times that will be the shortest actually run code.
Mishu is offline   Reply With Quote
Old 03-28-2012, 08:32 AM   PM User | #4
UD2006
Regular Coder

 
Join Date: Jul 2007
Location: Velsen Noord, Netherlands
Posts: 206
Thanks: 6
Thanked 0 Times in 0 Posts
UD2006 is an unknown quantity at this point
Quote:
Originally Posted by VIPStephan View Post
Hell yeah, this can be made shorter. But it would help to see your HTML in order to suggest a more generic traversal method.

The first thing that comes to my mind right now would be:
Code:
 $('.blok').mouseenter(function(){
   $(this).find('[id*=message]').show();
 }).mouseleave(function(){
   $(this).find('[id*=message]').hide();
 });
That’s the “contains” selector; alternatively you could use the “starts with” selector: [id^=message] or the “contains prefix” selector (string separated by hyphens): [id|=message]
Thanks, that did it.
UD2006 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:54 PM.


Advertisement
Log in to turn off these ads.