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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:27:05+00:00 2026-05-27T14:27:05+00:00

I wonder if it’s possible in jQuery/JS: If foobar is removed or gone from

  • 0

I wonder if it’s possible in jQuery/JS:

If “foobar” is removed or gone from the HTML code

<div id="foo">
 <div id="line1">...</div>
 <div id="line2">foobar</div>
</div>

I’d like to automatically add “new”

<div id="foo">
 <div id="line1">...</div>
 <div id="line2">new</div>
</div>

I’d like to have “new” displayed when someone deleted “foobar” from #line2. It’s for attribution purposes.

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

    If you make the text the value of an input you can bind to it’s change event:

    <div id="foo">
       <div id="line1">...</div>
       <div id="line2"><input type="text" value="foobar" /></div>
    </div>
    
    $('#line2').children('input').on('change', function () {
        var $this = $(this);
        if ($this.val() == '') {
            $this.val('new');
        }
    });
    

    Here is a demo: http://jsfiddle.net/u9Twu/

    Note that if you want to programatically change the value of the input you have to manually call .trigger('change') on the input after you change it’s value for the change event handler to run.

    Also note that .on() is new in jQuery 1.7 and in this case is the same as .bind().

    UPDATE

    var $foo   = $('#foo'),
        $line2 = $foo.children('#line2');
    
    if ($line2.length == 0) {
        //no #line2 element is found
        $foo.append('<div id="line2">new</div>');
    } else if ($line2.text().length == '') {
        //#line2 element is empty
        $line2.text('new');
    } else if ($line2.text() != 'foobar') {
        //#line2 element does not contain only the string: foobar
        $line2.text('new');
    }
    

    Here is a demo: http://jsfiddle.net/u9Twu/1/

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

Sidebar

Related Questions

I wonder if it is possible to supress errors in PHP like when redeclaring
I wonder whether someone has an idea for how to count combinations like the
I wonder if there is a way for me to SCP the file from
I wonder how it would be possible to implement a positional inverted index in
I wonder whether it is possible to change the display server IP of a
I wonder about the number of web pages I encounter that are HTML files,
i wonder what is the best solution to send an email from my app.
I wonder about shared preferences security. Is it possible to get access to sharedpreferences,
I wonder, why do I get not full response from my server ( Win
I wonder if there is an equivalent to the MySQL-Query: SELECT COUNT(*) FROM users

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.