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 7679971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:00:05+00:00 2026-05-31T18:00:05+00:00

In my code I have to divs with different names, except the last part

  • 0

In my code I have to divs with different names, except the last part of the names that’s the same (successMessage). When the div(s) appears in the code below is executed removing the div(s) after 1 second.

Of course, this is a good example of DRY because the functions is the same except for the name of the divs. So, what is the easiest way to merge these functions to one that affects both divs?

Thanks in advance!

    if ($('#MainContent_MovieEdit_MovieGenreEdit1_successMessage').length > 0) {
        setTimeout(function () {
            $('#MainContent_MovieEdit_MovieGenreEdit1_successMessage').fadeOut('slow');
        }, 1000);
    };

    if ($('#MainContent_MovieEdit_successMessage').length > 0) {
        setTimeout(function () {
            $('#MainContent_MovieEdit_successMessage').fadeOut('slow');
        }, 1000);
    };
  • 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-05-31T18:00:06+00:00Added an answer on May 31, 2026 at 6:00 pm
    $('#MainContent_MovieEdit_successMessage, #MainContent_MovieEdit_MovieGenreEdit1_successMessage')
     .delay(1000).fadeOut('slow');
    

    There is no need for the if statement, if jQuery couldn’t find the elements, nothing will happen. The multiple selector [docs] will select both elements and .delay() [docs] offers a nice way to defer the execution of fadeOut.

    Another possible selector would be the attribute starts with selector [docs]:

    $('div[id^="MainContent_MovieEdit_"]')
    

    assuming the elements are div elements. Of course you can also wrap this in a function if you really wanted to:

    function fadeOut($elements) {
        $elements.delay(1000).fadeOut('slow');
    }
    
    fadeOut($('div[id^="MainContent_MovieEdit_"]'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this code below to show different divs when i choose certain radio
I have divs that are initially closed. The code below then controls the opening/closing:
I have a slider that rotates between different divs nicely with the following code:
I have 3 different divs with images in them. Each div with a different
I'm attempting to load different content into a div, the refresh code I have
My code has some divs that appear on the screen based on different actions.
i have the following jquery code. basically i will have several overlapped divs and
I have ASP.Net code generating my button's HTML for me using divs to get
I have the following code snippet embedded into some of my divs so when
Put it another way: what code have you written that cannot fail. I'm interested

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.