Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8700733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:14:08+00:00 2026-06-13T02:14:08+00:00

I am trying to truncate div headers. My function is buggy because of, I

  • 0

I am trying to truncate div headers. My function is buggy because of, I believe, variable scope.

Here is my function broken down:

  1. Set variables so they are scoped properly
  2. Loop through each div:
    • Save long title
    • Create truncated title
    • Set truncated title
  3. On mouse enter, use full title
  4. On mouse leave, use truncated title

How can I rearrange this function so the variables are carried across correctly?

jQuery(document).ready(function($) {
    eventHover();
}); 


function eventHover(){

    // Set var scope
    var title = '';
    var shortText = '';
    var longText = '';

    // Change long titles to short version
    $('#event_promo .panel').each( function() {
        title = $(this).find($('h3 a'));
        longText = title.html();

        shortText = jQuery.trim(longText).substring(0, 50)
            .split(" ").slice(0, -1).join(" ") + "...";

        title.html(shortText);
    }); 

    // Add hover class and change title to long version
    $('#event_promo .panel .trigger').mouseenter(function () {
        $(this).parent().addClass('hover');
        title.html(longText);
    });

    // Remove hover class and revert title to short version
    $('#event_promo .panel .trigger').mouseleave(function () {
        $(this).parent().removeClass('hover');
        title.html(shortText);
    });

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:14:10+00:00Added an answer on June 13, 2026 at 2:14 am

    I would suggest using the data() method over “global” variables:

    $('#event_promo .panel').each( function() {
    
        var $title = $(this).find('h3 a');
        var longText = title.html();
    
        var shortText = jQuery.trim(longText).substring(0, 50)
            .split(" ").slice(0, -1).join(" ") + "...";
    
        $title.html(shortText);
    
        $(this).data({ shortText: shortText, longText: longText });
    }); 
    

    Handle the data:

    $('#event_promo .panel .trigger').mouseenter(function () {
    
        var longText = $(this).closest('.panel').data('longText');
        var shortText = $(this).closest('.panel').data('shortText');
    
        //do stuff with the texts here
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to truncate some code here and running into a problem: <script type=text/javascript> $(function()
I am trying to use the truncate filter on 2 variables (rsstitle and rssdescription),
I'm trying to truncate a decimal part. I mean create a function which do
I am trying to use the truncate function in scheme and DrRacket keeps issuing
Trying to copy the msdn refernce here doesn't work and gives an error I
Am trying to use animate marginLeft on text within a div, which is itself
I've been trying the String#truncate method provided by Rails 3: irb(main):001:0> abcde.truncate(1) => abc...
Im using DoctrineFixturesBundle and trying to load fixtures app/console -e=test doctrine:fixtures:load --purge-with-truncate All data
I'm trying to figure out how to truncate the first paragraph, and I've tried:
I'm trying to truncate some text in PHP and have stumbled across this method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.