Unable to locate the element with the specified id: <path>

I am aiming to dynamically change the fill attribute of a specific <path> element when a page loads.

Here is the detailed information about the path element:

<path xmlns="http://www.w3.org/2000/svg" 
style="fill:#ff0000;stroke:#000000;stroke-width:0.26458332px;stroke- 
linecap:butt;stroke-linejoin:miter;stroke-opacity:1;fill-opacity:1" 
d="M 71.895501,10.754349 94.88068,-28.80154 112.52047,12.892505 Z"
id="element"/>

This <path> element resides within a map.svg file that I embed in my HTML using an <object> tag, as suggested by a helpful reply on StackOverflow.

index.html

Below is the content of the HTML file:

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>Dental Ordination</title>
        <script src="svg.min.js"></script>
        <script src="jquery.min.js"></script>
    </head>
    <body>
        <object type="image/svg+xml" data="map.svg"/>
        <script src="main.js"></script>
    </body>
</html>

main.js

Here is an excerpt from my JavaScript file:

$(document).ready(function () {
    $("#element").attr("fill", "blue");
});

Upon execution, the fill color of the path does not change as expected. To troubleshoot this issue, I included

console.log($("#element").attr("fill"));
in the script to check its return value, which turned out to be undefined.

Answer №1

Why not change this code snippet:

$(document).ready(function () {
    $("#element").attr("fill", "blue");
});

to something like this instead:

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById('element').style.fill = 'blue';
})

The reason for this adjustment is that the element does not possess a fill attribute, but rather a fill style.

In regards to your follow-up inquiry, consider:

console.log($("#element").attr("fill"));
. This command returns undefined as there is no attribute called fill. Instead, you can use console.log(element.style.fill).

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 steps can be taken to resolve the delay in transition when clicking on "read less

I implemented a transition effect for the Read More and Read Less buttons, however, there seems to be a delay on the transition when clicking on the Read Less button. This delay was not intentionally set by me. How can I resolve this issue and also apply a ...

Using electron cookies in Angular involves integrating Electron's native cookie functionality into an

I am currently dealing with electron and looking to implement cookies conditionally in my project. If the application is built using electron, I want to utilize Electron Cookies; otherwise, I plan to use Angular Cookies. However, I'm encountering diff ...

What is the best way to send data from ajax to a django view?

I have a script for ajax that retrieves the public key and sends other details to the Stripe server for payment. However, I am struggling with sending the dynamic value of the price to item_line. fetch("/config/") .then((result) => { return re ...

Make sure the division remains fixed even when the window is resized

Having a fixed position and bottom set to 0, I want my div to display at the bottom of the window. The issue arises when the window is resized, causing the div to move up and into other elements. For instance, when opening the console box in Chrome, the f ...

Alter the classification of the tag

I am trying to modify the class of a tag upon clicking it. For reference, here is the jsFiddle link: http://jsfiddle.net/9u8Nc/ This is the HTML code I have: <ul class="tags"> <li> <a href="javascript:void(0)" class="tag">Adventure&l ...

The compatibility between V-model and TinyMCE is unreliable

I've been working on integrating Vuejs and TinyMCE, using the @tinymce/tinymce-vue package for the Vue integration. I followed all the instructions and everything seems to be functioning properly - I can write, insert images... everything except for t ...

Guidelines for dynamically passing HTML attribute values into a CSS selector using a variable in Protractor

After successfully locating the button on the row using its value stored in the "title" HTML attribute, I am now faced with the task of passing a dynamic value to this attribute. To achieve this, I have declared it as a variable. However, I am unsure about ...

The Like and increment buttons seem to be unresponsive when placed within a FlatList component

Issues with the like and increment button functionality within the FlatList Here are my constructor, increment, and like functions: constructor(props){ super(props); this.state = { count: true, count1: 0, }; } onlike = () => ...

A step-by-step guide on dynamically updating the placeholder attribute of an element using a fed id

I'm completely new to jQuery and I have a feeling that I'm making numerous mistakes in my code. As part of a project, I am creating a small search page for esports teams. The user selects their team from the homepage, which is then transferred t ...

How can I retrieve the POST values using xmlhttp?

This is an example of AJAX functionality. xmlhttp.send("fname=Henry&lname=Ford"); UPDATE I am looking to extract values from text/input fields after the equals sign. The current setup shows the values directly written out. How can I retrieve and dis ...

unable to show image retrieved from JSON data

Looking to showcase the data from my JSON objects, which are displayed in 2 images below https://i.stack.imgur.com/yhqFy.png https://i.stack.imgur.com/DUgFO.png In the data, I have properties like c_name, max_slots, and an array slots. Within the array, ...

Tips for ensuring a function in Angular is only executed after the final keystroke

I'm faced with the following input: <input type="text" placeholder="Search for new results" (input)="constructNewGrid($event)" (keydown.backslash)="constructNewGrid($event)"> and this function: construct ...

"Transferring data from Ajax to PHP with a GET request: A step-by

I have created an AJAX script that passes the user's selected value from a combo box to PHP and also handles the submit button. AJAX: <script> function sendUserData(userType) { var value = $('#userType').val(); var page=& ...

Issue on my website specifically occurring on Google Chrome and mobile devices

Hello, I seem to be having a CSS issue. Interestingly, my menu (burger menu) works perfectly fine on Firefox but is not functioning properly on Chrome and mobile devices. When I click the button, the menu doesn't open. Has anyone encountered a simil ...

Handling multiple patch requests using React and Redux when onBlur event occurs

Currently, I am using Redux-form for editing guest information. Whenever a field is left, the content of that field gets patched to the guest through a simple patch request and the store is updated accordingly. However, an issue arises when I use Google fo ...

Prepending a string to the key of a JSON object using JavaScript

Is there a way to add 'd:' before the key of a JSON object? Here is the JSON data: "data": { "aa": "value", "ab": "value" } The expected result should look like this: "d:data": ...

Combining Mouseover and Click Events in Vue JS

Having four pictures, I want to display a specific component when hovering over them. However, I also need to bind the click event so that clicking on the picture will reveal the component. The challenge is that I am unable to simultaneously bind two event ...

Extracting information from jQuery UI AutoComplete using a variable

Struggling to implement the jQuery UI Autocomplete feature, I'm faced with the challenge of integrating it with my PHP page that handles all JSON data requests. Using a switch statement in my PHP code, I determine the appropriate function to call base ...

AngularJS and numerous parallel indexed tables side by side

I need help with displaying two tables, one for profiles and the other for rates. The rates are connected to profiles through the rate name in the JSON data. Is there a way to show these two tables side by side? And when a row is selected in the profile ta ...

Building a React Redux project template using Visual Studio 2019 and tackling some JavaScript challenges

Seeking clarification on a JavaScript + TypeScript code snippet from the React Redux Visual Studio template. The specific class requiring explanation can be found here: https://github.com/dotnet/aspnetcore/blob/master/src/ProjectTemplates/Web.Spa.ProjectT ...