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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:01:20+00:00 2026-05-28T14:01:20+00:00

My situation is as follows: I have a script file ( somescript.js ) that

  • 0

My situation is as follows: I have a script file (somescript.js) that is included in the page and another snippet of code in the same page. On the page I have two buttons, the first one calls the snippet of code and it removes an element with remove() and the other calls a function from the script file, that checks if the respective element that was removed is still present. The problem is that it still finds that element. How can I solve this?

The snipped of code is:

<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script language="javascript" type="text/javascript" src="somescript.js"></script>
<script language="javascript" type="text/javascript">

$(document).ready(function() {
    /* used by the second button to call the checking function */
    $("#button2").live("click", function(e){ someFunction(); });

    /* if the control is here, it works, but if it's called from the inserted file (somescript.js), it doesn't */
    $("#button2").live("click", function(){
        if ($("#somediv").length == 0) {
            alert("Element missing");
        }
});

    /* used by the first button to remove the element */
    $("#button1").live("click",function() {
        $("#somediv").remove();
    });
});

</script>

While the function from the somescript.js file is:

function someFunction() {
    if ($("#somediv").length > 0) {
        alert("Alert message");
    }
}

And I’m getting the alert message. What am I doing wrong?

Update:

I’ve added the same code in the snippet of code inside the page (see the code above) and this way it works. But it still doesn’t work when it resides in the included file (somescript.js). What am I missing?????

  • 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-28T14:01:20+00:00Added an answer on May 28, 2026 at 2:01 pm

    Here is what you want to do.

    if ($('#someDiv') === undefined || $('#someDiv').length == 0) {
        alert('element does not exists');
    }
    

    The reason you should check for undefined instead of length is because in some cases if the element doesn’t exist how will .length exist? You will get an error saying ‘length’ is an invalid function of undefined.

    Edit:
    Also remove the language=javascript from your script tags. It should just be
    <script type="text/javascript" src="somefile.js"></script>

    Also if you are still getting the message you may want to console.log($('#someDiv')); to see what is going on. It could be you have 2 elements with the same id and when one is removed your script is seeing the last remaining element with that ID. Since ID’s are supposed to be unique when you do a .remove() only the first element with the given ID is removed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The situation is as follows: I have a ViewModel that has a property that's
Interesting situation. I have a Html.Textbox() that I render from a view as follows:
the situation is as follows, i have a page to add/update user in the
I have a situation as follows <body> Test<br /> test<br /> test1<br /> </body>
Picture the following situation. I have an XML document as follows, <Form> <Control Type=Text
Situation: I have a simple XML document that contains image information. I need to
I have a PHP mail script that successfully sends emails to everything BUT GMail
The situation is as follows:- We have n number and we have print them
The current situation is as follows: We have an production .net 3.5 WCF service,
Alright here's the situation, I have an application written in the Zend_Framework, that is

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.