Eliminating and restoring the background in a form field when it is in focus

/* This function clears the default text in a field when clicked on */
$('#edit-field-activity-notes-und-0-value')
    .each(function() {
        $(this).data('default', this.value);
       var mybackground = $(this).css('background'); 
    })
    .focusin(function() {
        // If the value is the default, clear it
        if ( this.value == $(this).data('default') ) {
            this.value = '';    
            $(this).css( 'background', 'white');   
        }
    })
    .focusout(function() {
        // If the value is empty, revert back to the default value and background
        if ( this.value == '' ) {
            this.value = $(this).data('default');
           this.css = $(this).css ('background', mybackground); 
        }
    });

What exactly is happening here? $(this).css( 'background', 'white'); successfully changes the background color but retains other styling attributes like no-repeat

The lines marked with ** are not functioning as intended.

Answer №1

In my opinion, the modification required is:

 this.css = $(this).css ('background', mybackground);

This should be replaced with:

 $(this).css ('background', mybackground);

Answer №2

The scope of your mybackground variable is not within the focusout function, and assigning a variable to the result of $.css() is unnecessary.

Additionally, it would be better to set the background-color property instead of just background, as using background will reset all unspecified background properties (such as background-repeat, background-image, etc) to their default values. You can try this revised code:

var mybackground;
$('#edit-field-activity-notes-und-0-value').each(function() {
    $(this).data('default', this.value);
    mybackground = $(this).css('background');
})
.focusin(function() {
    if (this.value == $(this).data('default')) {
        this.value = '';    
        $(this).css('background-color', 'white');   
    }
})
.focusout(function() {
    if (this.value == '') {
        this.value = $(this).data('default');
        $(this).css('background-color', mybackground);
    }
});

Answer №3

/* This script clears the default text in a field upon entry */
$('#edit-field-activity-notes-und-0-value')
    .each(function() {
        $(this).data('default', this.value);
    })
    .focusin(function() {
        if ( this.value == $(this).data('default') ) {
            this.value = '';    
            $(this).css('background', 'none');               
        }
    })
    .focusout(function() {
        if ( this.value == '' ) {
            this.value = $(this).data('default');  
            $('#edit-field-activity-notes-und-0-value').css('background', 'url(sites/all/themes/bartiksub/images/add-message.png) no-repeat center center');
            }else{
            $('#edit-field-activity-notes-und-0-value').css('background', 'url(sites/all/themes/bartiksub/images/add-message-after.png) no-repeat center center');
        }
    });

This approach is necessary because the image path is written directly into the element, not the CSS file. Therefore, the path must start from the root of the site for Drupal sites.

When restoring the original content saved in the variable, it's crucial to include the missing part of the path to ensure proper functionality.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combine two languages into a single <p>

I'm dealing with a WordPress website where I have a paragraph that contains two different languages. What I want to achieve is to display each language in a distinct font-family (font-face). For example: <p>למרות הכל its worth it</p& ...

select2 with customizable multi-column layout

Looking to create a multi-column select2 with a dynamic number of columns by specifying the number of columns as a data attribute in the select tag, or utilizing another method. <select data-cols="2,6,4"> <!--note data-cols--> < ...

"Troubleshooting the issue of a null object when sending JSON to a WCF Rest Service

I am new to using REST, WCF, and JSON in my application and I'm having trouble with the 'POST' functionality. While the 'GET' method is working fine, the 'POST' method is giving me issues. After packing up my JSON data u ...

"Utilizing the flex box feature in IE 10 for efficient text trunc

.container { background: tomato; width: 100px; display: flex; } .text-left { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .text-right { } <div class=container> <div class="text-left"> title title title ...

Ways to effectively utilize the results from php functions triggered by ajax in our document

I have a mobile application that constantly updates latitude and longitude values to my PostgreSQL database. I am attempting to display these updated points on an OpenLayers map using jQuery and AJAX. However, I am encountering a problem: after clicking th ...

Strategies for avoiding a hover component from causing distortion to its parent component in React

I am encountering an issue with a hover component that is causing distortion in its parent component when displayed. Essentially, I need to prevent the hover component from affecting the layout of its container. Below is the code snippet: Styling for Lang ...

Eliminating 'related' elements with jQuery

I am facing an issue with deleting items in a list. The list contains two types of products, product-x and product-y. Whenever I delete one type of product, I want the equivalent product of the other type to also be deleted. I have tried different approac ...

Updating the default color of selected text within a webpage's content

Is there a way to modify the default blue color that appears when content is selected on a webpage? I am wondering how to change this selection color to a custom color of choice. ...

Unraveling the mysteries of the Bootstrap carousel script

Hi everyone, I'm a newcomer to the world of JS and jQuery. Recently, while examining the code in carousel.js, I stumbled upon this particular line: this.cycle(true) The cycle function is structured like this: Carousel.prototype.cycle = function ...

Unable to receive notifications within an AngularJS service

<!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <body> <div ng-app="canerApp" ng-controller="canerCtrl"> <button ng-click="click()"> ...

Head styles for tinyMCE

When viewing the tinyMCE example on the official website using Firefox, you may notice the editor blinking. This issue seems to only occur in Firefox, possibly due to external CSS files for the editor. I am considering adding all CSS rules directly into th ...

sending an array from one CodeIgniter view to another view via Ajax

In the following code segments of my application, myArray is an array where each element contains a few objects that I need to use in a second view. When I use alert(myJSON);, I am able to see the array in the alert window. However, when the view loads, i ...

Avoiding overlapping in setTimeOut when using jQuery.hover()

Looking to trigger an effect one second after the page loads, and then every 3 seconds in a loop. When the user hovers over a specific element with the ID #hover, the effect should pause momentarily. It should then resume 2 seconds after the user stops hov ...

Enhancing jQuery Mobile listview with voting buttons on each item row

I am looking to incorporate 2 vote buttons within a jQuery mobile listview, positioned on the left-hand side and centered within each list item. While I have managed to achieve this using javascript, my goal is to accomplish it without any additional scrip ...

How to use jquery and ajax to retrieve an array of data and show it on the screen

I am facing an issue with my ajax request. Actually, I am unsure of how to fetch multiple records. I attempted the following: $rqt = "SELECT a,b,c from table"; $res = mysql_query($rqt); while ($data = mysql_fetch_assoc($res)): $objet = $d ...

Pattern Matching for Website Addresses

I'm having trouble with the regular expression I'm using to validate URLs. The expression is /\b(http|https)?(:\/\/)?(\S*)\.(\w{1,45})/ig It seems that the regular expression only works for URLs up to a certain leng ...

Displaying a loading screen while a jQuery ajax request is in progress

Currently, I am attempting to display a loading div while waiting for an ajax call to finish. Despite experimenting with various methods, I have not been able to consistently achieve the desired outcome. In my present code, everything functions properly o ...

Utilize jQuery autocomplete to limit user input in a text box

I'm currently implementing jQuery UI's autocomplete feature to populate a form input with accurate data from a list of over 1000 values. The autocomplete functionality is functioning correctly, but I am unsure how to restrict the field's va ...

Ways to customize the background color of child cells in a table

I've implemented material-table in this UI and I'm currently working on customizing the style of specific cells only when the onTreeExpandChange is true. Essentially, my goal is to change the background color for cells 2 & 3. Check out the s ...

firefox compatibility issues with jquery ajax

Trying to establish a connection with a URL and expecting a large object in response has proven to be challenging. The process appears to function smoothly until an error occurs during the return reply, triggering the .ajax.error() function. Unfortunately, ...