Layering properly with z-index in versions of IE older than IE9

I am currently working on developing a straightforward plugin to create HTML5 placeholders for Internet Explorer 9 and earlier versions. My current issue is that even though I have assigned a higher z-index to my input field, the label element still appears "on top" of it, causing a problem with focus/click functionality.

Below is the JavaScript code I have written:

$.fn.placeholder = function () {
    return this.each( function () {
        var elem = $( this );

        if ( elem.is( "input" ) || elem.is( "textarea" ) ) {
            var placeholder = elem.attr( "placeholder" ),
                wrapper = $( "<span></span>" ).css( {"position":"relative", "display":"inline-block"} ),
                label = $( "<label></label>" ).text( placeholder ),
                border = elem.outerWidth() - elem.innerWidth(),
                paddingH = elem.innerWidth() - elem.width(),
                paddingV = elem.innerHeight() - elem.height(),
                left = elem.outerWidth( true ) - elem.width() - Math.floor( (paddingH / 2) ) - border,
                top = elem.outerHeight( true ) - elem.height() - Math.floor( (paddingV / 2) ) - border;

            label.css( {
                "position":"absolute",
                "top":top,
                "left":left,
                "color":"#a9a9a9",
                "z-index":10
            } );
            elem.css( "z-index", 1000 ).css( "position", "relative" );
            elem.wrap( wrapper );
            elem.parent().prepend( label );

            elem.focus( function () {
                elem.parent().find( "label" ).hide();
            } );
            elem.blur( function () {
                elem.parent().find( "label" ).toggle( $.trim( elem.val() ).length == 0 );
            } );
        }
    } );
};

//somewhere on document.ready
$("input").placeholder();
$("textarea").placeholder();

HTML:

<div>
    <input type="text" placeholder="Some placeholder" name="name">
    <textarea name="request" placeholder="Other placeholder"></textarea>
</div>

CSS:

input {
    padding: 5px 10px;
    background: transparent;
}

textarea {
    resize: none;
    padding: 30px;
    background: transparent;
}

Under Internet Explorer, when attempting to click on an input/textarea field, the cursor does not change to indicate text entry, and clicking in that area has no effect. You must click outside of the label's range to successfully focus on the input field.

JSFiddle: http://jsfiddle.net/7SXBC/2/

Answer №1

It seems like using the z-index property may not achieve the desired result in this case. If you want the input element to be on top of the label, the label may end up being invisible altogether.

Instead of manipulating the z-index, consider binding the click event of the label to focus on the input or textarea elements. You can do something like this:

label.click(function(e){
    elem.trigger('focus');
});

UPDATE: It's possible that your issue is related to a known bug:

Input boxes with transparent background are not clickable in IE8

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

Tips for modifying the text color of radio button choices in HTML and CSS

I have set a black background image for my HTML page. How can I change the radio button labels/texts to white, similar to the questions? This is the code snippet that shows how it currently looks on the page: View Image <hr&g ...

What is the best way to align bootstrap grid columns perfectly centered beneath a set of three columns?

I'm attempting to create a container grid layout similar to the one shown below using Bootstrap 4. The layout consists of three evenly spaced boxes at the top, followed by two additional boxes centered and evenly spaced below them: https://i.sstatic. ...

Invoking a web method from the client side using jQuery AJAX - not simply making a call to the method but actually entering the page load

When I click the edit button, it triggers the button click function in JavaScript. However, when the ajax() function is executed, instead of directly calling the update() function in webform as specified by [ url: "WebForm1.aspx/update",], the control go ...

The fillOpacity and opacity properties appear to be malfunctioning

Note: While I understand that image masking can solve my issue, I prefer not to use it as it would require me to use my image as a background and could present accessibility challenges. I am specifically looking for solutions involving clip-path. Issue: T ...

Tips on resetting the position of a div after filtering out N other divs

Check out this code snippet. HTML Code: X.html <input type="text" id="search-criteria"/> <input type="button" id="search" value="search"/> <div class="col-sm-3"> <div class="misc"> <div class="box box-info"> ...

What is the best way to send data to PHP while moving objects between different div elements?

I have a situation where I have two parent divs containing dynamic children divs, and I want to enable POST requests to PHP when items are dragged from one side to the other (and vice versa). Here is the Javascript code I am using: function allowDrop( ...

Ways to retrieve a single variable periodically and refresh it at intervals

One common issue faced by maintenance employees at my company is losing their log entry if they receive a call or text while filling out the daily form on their phones. To solve this problem, I came up with a solution to store the form values in PHP SESSIO ...

How can I stop a hyperlink from activating Jquery?

A script was created to highlight any <TR> element when a user clicks on it, and it is functioning correctly. <script> $(document).ready(function () { $(document).on('click', 'tbody tr', function (e) { ...

Get the identification number from a div located within an array of JSON objects using only JavaScript

I've scoured the internet for solutions to my problem, but nothing seems to work as I need it to. I've attempted various methods, including using href and button with the router, but unfortunately, none of them fit the specific requirements in th ...

Utilizing Jquery for Enhancing Annotations

I am in the process of developing a website for essay writing tests. I have implemented a feature where users can input their essays using a text area, and now I want to be able to make comments on that text similar to PDF annotations or highlighting. I at ...

Having trouble initiating an AJAX call in Django and returning values to my form

Seeking assistance to run an AJAX call for a quick calculation in my django view and display the result within a span tag on my HTML page. New to Javascript, struggling with triggering my AJAX call. Below is my HTML and JS code: <input type="text" nam ...

Tips for removing special characters from HTML?

I'm currently grappling with the concept of the filter My situation is as follows: $htmlData includes a user-entered html string formatted like this $htmlData = array('<em>test</em>', 'text here','\n') ...

Is Webkit's Overflow Scrolling feature hiding the divs on your website?

I recently implemented a design for my website where the content is contained within an absolute positioned div that takes up the entire screen. However, I noticed that the scrolling on this div felt clunky and not as smooth as I would like it to be. After ...

The equation of Laravel 5.4 and Ajax results in the error code 401 for Unauthenticated

Every time I try to set a route from my api.php File, I encounter a 401: Unauthenticated Error. Here is the route: Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function () { Route::post(&a ...

Only fire the click event of the parent div if none of the child elements are clicked

Here is a fiddle I've prepared for reference: https://jsfiddle.net/9m4bgyq8/16/ The issue I am facing is that I am attempting to wrap a checkbox and label within a div, and apply a click event to the div. The intention is for the click event to only ...

Enhancing jQuery functionality: Ensuring newly added elements are aware of existing functions

I need assistance with my HTML table. Each row (tr) contains a value, for example 200$, and a delete button. How can I ensure that each new row added automatically knows the recent functions without having to call them every time? $(function () { $(&ap ...

The div "tags" cannot be positioned beneath the photo as it is automatically located alongside the image inside the div

Is there a way to place the "Tags" div beneath an image, creating a bar of tags below it? I've been struggling to position it properly without disrupting the flow of the page. Using absolute positioning is not an option as it would break the layout. A ...

Issue with Angular: boolean value remains unchanged

Currently, I'm encountering an issue with my application. My objective is to establish a list containing checkboxes that toggle their values between true and false when clicked. Sounds simple enough, right? Below is the HTML code snippet: <l ...

Adjust the minimum and maximum dates of a ui datepicker dynamically

Expected it to be easier than it actually is. Task: 1)disable future days. (MaxDate set as today - completed) 2)get date from js variable and dynamically set it as a minDate - how? Main function used for multiple pages: jQuery( function($){ ...

the iframe is not properly displaying the embedded responsive page

The primary webpage incorporates the responsive mobile page created with the JQUERY SUPERSLIDES plugin. The mobile page appears correctly when viewed directly on an iPhone using Safari. However, when it is embedded in an iframe, it does not display as expe ...