Internet Explorer 9 successfully executing the getAttribute method

I am trying to set a custom attribute called titleDiv using the setAttribute method

Here is an example of the HTML code:

<div id="Object" titleDiv="blank"></div>

This JavaScript code usually works, but it seems to have some issues with IE 9:

var Object=document.getElementById('Object');
Object.setAttribute("titleDiv","myDiv");

Unfortunately, this solution does not seem to work properly in Internet Explorer 9 when using getAttribute as well.

I heavily rely on these custom attributes in my code and I cannot change the structure. Are there any alternative approaches that can ensure compatibility with IE?

Thank you for your help.

Answer №1

Revised Response following changes to the question:

The usage of the identifier Object is strongly discouraged. It is advisable to modify both the id and the variable name you are using. For instance:

<div id="theObject" titleDiv="blank"></div>

and

var theObject=document.getElementById('theObject');
theObject.setAttribute("titleDiv","myDiv");

Avoid using reserved JavaScript functions such as Object, Function, Date, etc., as values for the id attribute due to their global namespace implications. This practice can lead to confusion among developers and should be avoided as variable names as well.

Although changing the id and variable name is recommended, it may not necessarily solve the problem as noted below.


Initial Answer:

The setAttribute method should be called on an actual DOM element instance, rather than using the built-in Object function.

If you target a specific DOM element, the functionality works (even on IE9):

(function() {
  // Obtain the corresponding DOM element
  var target = document.getElementById("target");

  // Set the attribute
  target.setAttribute("titleDiv","myDiv");

  // Displaying the result
  display("Result: " + target.getAttribute("titleDiv"));

  function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = String(msg);
    document.body.appendChild(p);
  }
})();

Live Demo | source

Inspect the HTML tab using F12 developer tools to verify that the div indeed has the attribute. Ensure to use dev tools instead of "view source," which displays the initial server-sent HTML, not the current DOM state.


A side note: Employing custom attributes without adhering to the data- prefix is not recommended. You might want to communicate this concern to those overseeing these decisions. Nonetheless, this issue is unlikely the root cause of your current dilemma.

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

This function is functional with jquery version 1.4.2 but encounters issues with jquery version 1.9.1

I have encountered an issue with a script that submits data to my database. The script worked perfectly on version 1.4.2, but the template I am using now requires version 1.9.1, so I updated my site accordingly. However, after the update, I am facing an er ...

Instructions on placing the bottom of an inline-block element flush with the bottom of a block element

In the scenario where I have the following HTML and CSS code: .something{ width: 200px; } .display-block { display: block; } .req-field{ float: right; font-size: 5px; } <div class="something"> <span class="display-block">First name ...

“Tips for positioning text in the middle using HTML and CSS”

I'm trying to customize the text "TITLE" in my HTML and CSS code, #headerf { background-color: #717571; overflow-x: hidden; position: fixed; z-index: 99999; } .headert { position: fixed; z-index: 9999999; width: 100%; height: 60px; ...

What is the best way to connect the elements in two separate arrays?

I have a scenario with two arrays and a variable: var Names = ['jack', 'peter', 'jack', 'john']; var Ids = ['1' , '2' , '3' , '4' ]; Also, I have this search varia ...

Ensuring that only one field is selected with mandatory values using Joi validation

Is there a way to create a validation rule utilizing Joi that ensures if valueA is empty, then valueB must have a value, and vice versa? My current approach involves using Joi for validating an array of objects with properties valueA and valueB. Below is ...

How can I make a basic alert box close after the initial click?

After clicking a button, I have a simple jQuery script that triggers a fancybox. However, the issue is that the fancy box does not close after the first click of the button; it only closes after the second click... Another problem arises inside the fancy ...

The animation for the login and registration modal is malfunctioning and not functioning as

I am currently working on implementing a login/register modal in React, inspired by a TypeScript example I came across. This is how the login section looks: https://i.sstatic.net/ECvv9.png The goal is to: When the "Sign up" button is clicked, it should ...

Implement JQuery to include a screensaver on your website

My website in asp.net c# displays the performance of a specific process on an LCD TV screen. The data is refreshed every 15 seconds using the code below: <div id="InfoTop"> <table width="100%" cellpadding="0" cellspacing="0"> ...

What is the best way to eliminate the space between columns?

I have a simple structure on BootStrap. <div class="container"> <div class="row"> <div class="col-md-4 blue-section" ><p>Some content</p></div> <div class="col-md-4 red-section"><p>Some content&l ...

Using backslashes to escape a JavaScript array elements

How can I modify the code below to properly escape HTML and strings in JavaScript arrays? I've been attempting to use a function called escapeHtml to add the necessary slashes, but it's not functioning as expected. Any help would be appreciated. ...

Aligning the email form to the center with the text-align center property:

I'm in the process of creating an app landing page using the Bootstrap framework (Bootstrap 4 alpha). I have used text-align:center in the jumbotron id to center everything, but the email form is not aligning properly. Is there a way to center align ...

Center alignment is not being applied to the SVG element

I am currently working with React and when my component renders, it displays an SVG inside a div. Here is a snippet of the code: render() { return( <div class="myClass"> <svg> ... </svg> </div> ); ...

Having difficulty creating a basic container

As a beginner in web development using html/css, I am eager to learn and create something simple. I want to design a responsive fixed box that always maintains a 20px distance from the viewport edges. The idea is that as the screen size changes, the box ...

When using Mongoose with Ejs, I am unable to view the comment I made when I navigate back to the post using GET. The comment only becomes visible after I have submitted it

After creating a comment, I can only see it when rendering the same page again. However, if I navigate to posts or myposts page and then return to the post where I created the comment, it disappears. Can someone please help me understand why this is happen ...

Is there a way to use TestCafé to simultaneously log into multiple services?

I've been experimenting with testcafé in an effort to simultaneously log into multiple services using the role mechanism. My goal is to have my tests logged into multiple services concurrently without switching between roles. While a guide on this t ...

A straightforward Node.js function utilizing the `this` keyword

When running the code below in a Chrome window function test(){ console.log("function is " + this.test); } test(); The function test is added to the window object and displays as function is function test(){ console.log("function is " + this.tes ...

Executing a post request after redirection in a node.js application

Can anyone help me with redirecting to a different URL from node js using the response.writeHead method? response.writeHead(301, {Location : <redirecturl>}) I want this redirection to be executed as a POST request, but it always defaults to GET. Is ...

How can I utilize Bootstrap 4 to ensure that an image remains centered on the page even when the page size is adjusted?

Looking to insert a thumbnail in front of a video using Jquery. I managed to add the image, but facing an issue when resizing the page as the thumbnail should be centered. Here is the desired layout: https://i.sstatic.net/IDOb9.png However, my current res ...

Guide on dividing and presenting ajax information into two separate div containers

I am attempting to showcase two sets of data on separate divs using ajax. Below is the code I am utilizing for this task. Here is the ajax implementation $(function () { $(".myBtn").click(function () { var id = $(this).data("id"); ...

Validating emails using React.js and HTML5

In my React component, I have an email input field: <input type="email" autoComplete="email" placeholder="Email" required value={this.state.formDa ...