In IE the OnFocus and OnBlur style changes are not functioning properly, while they work successfully in Fire Fox

Greetings everyone, I have defined the following styles in my CSS:

.on_focus {
    background-color:yellow;
}
.off_focus {
    background-color:white;
}

Now, within my form, there is this input field:

            <label>Last Name</label><br/>
            <input id="l_name" class="text" type="text" name="l_name" maxlength="20" onfocus="this.className='on_focus'" onblur="this.className='off_focus'"/><br/><br/>

While this functionality works perfectly in Fire Fox, it seems to be malfunctioning in Internet Explorer. In IE, the background color of the input field remains white regardless of whether it has focus or not. Can anyone shed some light on why this discrepancy exists?

Answer №1

You have the option to achieve this effect using only CSS, although it's not the only method available.

Here is an example of how you can accomplish this with CSS:

input[type="text"]:focus {background-color:Yellow;}
input[type="text"]:blur {background-color:White;}

Make sure to add the attribute selector for type=text in order for this styling to only apply to input tags of that type.

--UPDATE--

If compatibility with Internet Explorer is a concern, you may consider using a JavaScript solution like the one below:

<script type="text/javascript">
    function onFocus(element) {
        //element.style.backgroundColor = "Yellow";
        element.className = "on_focus";
    }
    function onBlur(element) {
        //element.style.backgroundColor = "White";
        element.className = "off_focus";
    }
</script>

Here is how you would implement this in your HTML:

<label for="l_name">Last Name</label> 
<input type="text" id="l_name" onfocus="onFocus(this);" onblur="onBlur(this);" /> 

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

What could be the reason why this function in jQuery is not functioning properly?

I am trying to duplicate the span element inside the `test` element using the .clone method. It seems like it works as expected. However, when I try to use .html in a `.click` function, it doesn't work. Can someone point out where I went wrong and sug ...

Display the Astro component based on the query of the current page's type

I am using Astro, GraphQL (Apollo Client), Typescript and React. Within my dynamic route: [...slug].astro file, I have a requirement to conditionally display a specific Astro component. I was able to achieve this using the following logic: {data.page.ty ...

Expanding the Bootstrap panel to a specific size with a scrollable panel-body

Is there a way to create a panel in bootstrap with specific maximum and minimum lengths? My goal is to set a fixed length for the panel body and enable a scroll bar if the text overflows that length. I've been attempting to increase the length of the ...

Activate the date-picker view when the custom button is clicked

Utilizing this library for date-picker functionality has been quite beneficial. I am currently working on a feature that involves opening the date-picker upon clicking a custom button. The default input is functioning properly. <input name="pickerFromD ...

The integration of Google Translate with Javascript on HtmlEditorExtender is experiencing difficulties and is not functioning properly

I implemented the use of a text box with ajaxtoolkit HtmlEditorExtender (Rich textbox) to translate English to Gujarati using Google translation Javascript. The translation function works perfectly with the regular text box, but encounters issues when used ...

Is it possible to retract a message on Discord after it has been sent?

I have a code that automatically sends a welcome message when a new member joins the guild, and I want it to be deleted shortly afterwards. Here is my current code: client.on('guildMemberAdd', (member) => { var server = member.guild.id; i ...

When you click the "Select All" button in a JavaScript dialogue box, all of the checkboxes on the main page will automatically be selected

After clicking a button on the main page to add a SKU, a new window opens with a search field, as shown in the image below. In this pop-up window, there is a "Check All" button that, when clicked, selects all checkboxes on both the pop-up window and the ma ...

I believe I am attempting to add some space to a row using CSS, or at least that's what I think I'm trying to achieve

Hey there, I need some help with adding an icon "services" section to my website. CSS is not my strongest suit, so I'm reaching out for assistance. My goal is to create blocks for the icons that don't touch the edge of the page and have a margin ...

In JavaScript, you can use the document.cookie property to delete specific cookie values identified by their names and values

Within my JavaScript code, I am working with a cookie that contains multiple names and values: "Token=23432112233299; sessionuid=abce32343234" When I download a file from the server, a new cookie is added to the document, resulting in the following cooki ...

Exploring the differences between the meta viewport and font size

My website includes the following code in the section: <meta name="viewport" content="width=990"/> This setting helps to display my webpage well on mobile devices, as using width=device-width causes issues with elements that have 100% width. Howeve ...

Angular checkbox filtering for tables

I have a table populated with data that I want to filter using checkboxes. Below is the HTML code for this component: <div><mat-checkbox [(ngModel)]="pending">Pending</mat-checkbox></div> <div><mat-checkbox [(ngModel ...

Create a function that can dynamically filter an array of objects and extract specific properties into a new array

Here is the input that I have: [ { "metadata": { "id": 1071, "name": "First" }, "languages": [ { "name": "Alpha", "details": [ { "city" ...

Selenium's WebDriver getAttribute function can return an object of type "object",

In my selenium script, I aim to extract text from table columns following the cell with the specified value. Although the script functions, I encounter an issue where getText() returns [Object Object] in my node.js console. I have attempted various method ...

What is the best way to display an additional component upon logging out in a React application that utilizes

Is there a way to render a different component when logging out of firebase? I am attempting to refresh the page to display the LoginPage. Here is my LoginPage that is shown when logging in with a different form. import React, { Component } from "react" ...

The close button on the modal vanishes on a real iPhone

The issue I am facing is that the Close Button appears when testing responsiveness in Chrome, but disappears on a real iPhone. When clicking on an image, the image gallery should appear. However, on an iPhone, only the previous and next buttons are visibl ...

What is the reason behind my resizer bar not changing color to green?

Whenever I resize the bar between the West and Inner Center areas, it turns green. However, the bar between the Inner Center and Inner South areas does not change color. How can I make it turn green when resizing, including adding the orange highlight for ...

Is there a way to accomplish this using Bootstrap?

Hello there! I have a piece of code that is working with both bootstrap and Pixedelic camera. Here it is: <section id="slider" class="container-fluid"> <div class="row fluid"> <div id="camera_wrap_1"> <div data-src="conten ...

Receive a positive or negative data message through Ajax Response

Exploring Ajax for the first time, I am eager to incorporate database interaction using AJAX in JQUERY by connecting to a PHP script. $(function() { $.ajax({ type: "POST", url: "response.php", ...

Blocking of Node.js Promises

I am currently new to Node/JS and am working on developing a password recovery page for an existing IT portal. The page involves searching through AD (ldap) and a DB where users have registered. The goal is to present users with options to authenticate and ...

Using Nightwatch.js to upload an image file

Currently, I am conducting front-end tests with nightwatch.js and utilizing the Chrome Driver. My specific focus is on testing the functionality of image uploading, which I believe can be done through the file input provided as there are callbacks that tri ...