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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:28:51+00:00 2026-05-15T15:28:51+00:00

Please see this jsFiddle: https://jsfiddle.net/Wmq6f/ I have a textarea that has a background image,

  • 0

Please see this jsFiddle: https://jsfiddle.net/Wmq6f/

I have a textarea that has a background image, it is removed on ‘focus’ and restored on ‘blur’ but when the content is present in the textarea it should not show in either case, but I can’t achieve that with this jQuery:

$('textarea').focus(function() {
   var $this = $(this);
   $.data(this, 'img', $this.css('background-image'));
   $this.css('background-image', 'none');
});
$('textarea').blur(function() {
    if($.trim($('textarea').val()).length){
         $this.css('background-image', 'none');
    } else {
        $(this).css('background-image', $.data(this, 'img'));
    }
});

Thanks for your help

  • 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-15T15:28:52+00:00Added an answer on May 15, 2026 at 3:28 pm

    The textarea tag isn’t closed which can be a source of some annoyance 🙂

    Also it might be helpful to factor out the logic from focus and blur callbacks into a separate function as the same checks need to happen on both events.

    1. if textarea is empty, show image
    2. otherwise hide image

    Another advantage of factoring this logic out in a separate function is that you can call this new function onload, which will initialize the textarea before any focus or blur event happen.

    On jsfiddle, when you choose the "onLoad" option on the left, the JavaScript code is automatically wrapped in a window.load callback, so you don’t have to specify it in code. Either that or choose the "No wrap" option to write the window.load event in code yourself.

    Fiddle update:

    function init(text) {
        text = $(text);
        text.data('img', text.css('background'));
        
        var update = function() {
            if(text.val() == '') {
                text.css('background', text.data('img'));
            }
            else {
                text.css('background', 'none');
            }
        };
    
        text.focus(update);
        text.blur(update);
    
        update();
    }
    
    init('textarea');
    
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see this : http://jsfiddle.net/ymSpY/ . If you can see <td> has an inner
Please see this fiddle: http://jsfiddle.net/ZWw3Z/5/ My code is: p { position: relative; background-color: blue;
Please see this: http://jsfiddle.net/uNbYu/1/ If you click submit, the button doesn't do anything, but
please see this example http://jsfiddle.net/7trcV/ what I'm trying to achieve is ability to put
Please check out this jsfiddle http://jsfiddle.net/rr4GH/1/ As you can see its a round edge
Please see the solution to another question provided by ud3323: http://jsfiddle.net/ud3323/ykL69/ . This solution
Please see http://jsfiddle.net/jaWjB/1/ The line break happens in between the second image and its
Please see this: My code looks like this now: http://jsfiddle.net/Bd9Ut/ As you can see
I need some help with this accordion. Please see the demo here. http://jsfiddle.net/a36RL/ $(document).ready(function()
Please see this http://jsfiddle.net/yv8uw/1/ When you click View all Comments the box opens up

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.