Validate the presence of a color attribute within the <font> HTML tag and apply a label if it exists using JavaScript

I am working with the following HTML code snippet:

<font color="#ff0000">מתחנת חוף הכרמל</font>

My goal is to verify if it includes the color style and, if so, add a label to it. The approach I attempted was unsuccessful:

if ($("font").css('color') == '#ff0000' ) {
   $("font").attr('role':'heading');
} else {
   console.log('not working');
}

Any suggestions or advice on how to achieve this? If there is a way to accomplish this using only pure JavaScript, that would be even better.

Answer №1

Give this a shot:

<font style="color:#ff0000;">From HaCarmel Beach Station</font>

var col = document.getElementsByTagName('font')[0].style.color;
 if(col=='rgb\(255, 0, 0\)') {
   document.getElementsByTagName('font')[0].setAttribute('role','heading');
 } else {
   console.log('perform actions...');
 }

Take a look at the demo: https://jsfiddle.net/r2sb0hjn/

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 showcasing several thumbnails simultaneously as you upload images on a webpage with HTML and JavaScript

function displayImage(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('#selectedImage') .attr('src', e.target.result) }; reader.read ...

When a form is submitted, the PHP file is opened in a new page instead of running the script

I have developed a contact form that needs to be validated through ajax PHP before being sent via PHP email. However, upon clicking submit, the page refreshes and redirects to a new page: mypage.com/form_process.php Additionally, the stylesheet is not l ...

Is it necessary to use JS/JQ to trigger PHP form data?

Can PHP files/functions be executed without reloading the page? It can be quite disruptive when developing a chat app and every time you send a message, the entire page refreshes. I attempted to use AJAX but it didn't work. Is it not possible to send ...

Replace FormControl.disabled by using a CSS or HTML attribute

I have a customized angular date picker where I want to restrict user input by disabling the input field and only allowing selection from the date picker. However, using the "disabled" attribute on the input element is not an option due to conflicts with t ...

Duplicate the form field and add a button beneath the newly generated elements

Clicking the "Add New" button below will add new fields to the form. While everything is functioning properly, I would like the newly created fields to appear above the button instead of below it. How can this be achieved? (function($) { "use strict" ...

Arrange the elements in an HTML table with the first and last columns on the left and right sides

Is it feasible to have the first and last columns of an HTML table fixed while allowing the middle columns to be scrollable? (This functionality is achievable in jQuery DataTables, but what about a basic HTML table?) ...

What is the best way to wait for a DataTable() to finish loading data via ajax and

In the JavaScript code I've written using jQuery, I have a DataTable that needs to be loaded with data before any further actions can be taken. Here is my current code: function f () { /* ... */ $('#my_datatable').DataTable().ajax. ...

Initiate a click event on an anchor element with the reference of "a href"

I'm currently facing an issue with clicking a href element on my HTML website. The click event doesn't seem to be triggered. CODE: HTML CODE: <div class="button" id="info" ></div> <ul id="game-options"> <li><a ...

Achieving a specific length in various classes within a jQuery plugin can be accomplished by implementing targeted

I am in search of a jQuery plugin that can display the count of elements within each class separately. For example, I would like the output to be as follows: the result is: 4 the result is: 6 However, when I try this code snippet, the actual result I get ...

Discovering a hidden DIV beneath other DIVs without disrupting their visibility - a step-by-step guide

I have created four separate DIVs that divide the screen equally into quarters. Additionally, there is a full-screen DIV designated for canvas. My goal is to ensure that these DIVs do not overlap each other - meaning if a user clicks on a link within one ...

Different method for adding child elements to the DOM

When creating a DOM element, I am following this process: var imgEle = document.createElement('img');     imgEle.src = imgURL;             x.appendChild(imgEle); Instead of appending the last line which creates multiple img elements ev ...

Error: Unable to retrieve the specified ID

One unique aspect of my backbonejs app is the model structure: var Store = Backbone.Model.extend({ urlRoot: "/stores/" + this.id }); This is complemented by a router setup like so: var StoreRouter = Backbone.Router.extend({ routes: { 's ...

Error: Unable to locate module '@emotion/react' in 'E:frontend ode_modules@muistyled-engine' directory

I've been trying to import a box component from @mui/material/Box. After installing Material-UI 5 using npm i @mui/material, I encountered the error message Module not found: Can't resolve '@emotion/react' in 'E:\frontend&bsol ...

Transitioning from using lerna to adopting pnpm

We are in the process of transitioning our project from Lerna to PNPM and we currently have a script that we run. Here are the commands: "postinstall": "npm run bootstrap" "bootstrap": "lerna bootstrap --hoist", &quo ...

What is the best way to apply CSS hover effects to specific cells within a table?

Is there a way to selectively apply CSS hover effects to only specific cells in a table? And can I disable it on the cells where I don't want it? td:hover { border-style:dotted; border-color:#F60; border-width:medium; border-left-styl ...

How can node.js be utilized to create a rapid polling and ajax server for database updates in a manner reminiscent of Google Sheets?

I'm hoping that my title isn't too lengthy. A straightforward question/title: What settings should I utilize to leverage node.js as a lightning-fast polling and ajax server for rapidly updating and verifying changes on both client-side and serv ...

A more concise method for verifying if the values of several inputs are empty using jQuery

let title = $('#plustitle').val().trim(); // input type text let tags = $('#plustags').val().trim(); // input type text let story = $('#plustory').val().trim(); // textarea I want to check if any of the variables above are e ...

Efforts to extract data from HTML tables using rvest techniques may result in receiving

Although I've had success using rvest to scrape data from html tables before, I'm running into an issue with a specific website: . When I execute the following code snippet: url <- "http://www.sanzarrugby.com/superrugby/competition-stats/2016 ...

Some pages are compatible with JS while others are not... Although, the file can be found in the page source

I have limited knowledge in javascript, so I often find myself copy-pasting code. Currently, I am working on a website that includes a navigation sidebar. I have implemented a script to toggle a class that sets the display property to none. $(document).rea ...

Error in code - message gets sent immediately instead of waiting for timeout to occur

There seems to be an issue with the code where the message is sent instantly instead of waiting for the specified timeout duration. Based on the code, it should wait for the time mentioned in the message. I'm puzzled as to why it's not functioni ...