Find the attribute value, locate corresponding values on different elements, and make changes to the CSS styling

Currently, I am attempting to retrieve the value of an attribute called 'data-location', and then locate that same value somewhere else in the form of an ID. Once matched, I aim to modify the CSS for that specific value.

Although I have a code snippet that I believe should work, I am unsure about how to handle different values for various elements and implement changes accordingly. As a result, I have separated the sections into divs with classes to distinguish them from each other.

Any insights or suggestions are greatly appreciated!

var getvalue = $('ul#test').find('li').attr('data-location');

if( $('div.other div').attr('id').val() == getvalue ) {
 $( this ).css('background-color','red');
};
#thebirchplot3 {
 padding:30px;
 background:#fff;
}
#theashplot3 {
 padding:30px;
 background:blue;
 color:#fff;
}
#thediveplot1 {
 padding:30px;
 background:yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h1 id="title">Change CSS based on values</h1>
<ul id="test">
    <li data-location="thebirchplot3">one</li>
    <li data-location="theashplot3">two</li>
    <li data-location="thediveplot1">three</li>
</ul>
<ul id="show">
    <li data-location="theashplot3">one</li>
    <li>two</li>
    <li>three</li>
</ul>
<ul id="display">
    <li data-location="thediveplot1">one</li>
    <li>two</li>
    <li>three</li>
</ul>
------
<div>
  <div class="other">
    <div id="thebirchplot3">
    adfasdfasfsafdsafdsfsadfdsafdsafdsfafadsf
    </div>
    <div id="theashplot3">
    adfasdfasfsafdsafdsfsadfdsafdsafdsfafadsf
    </div>
    <div id="thediveplot1">
    adfasdfasfsafdsafdsfsadfdsafdsafdsfafadsf
    </div>
  </div>
</div>

Answer №1

Follow the instructions below for proper implementation:-

Here is a practical example:-

$('ul#test').find('li').each(function(){ // loop through each li element
  var ultest = $(this); // assign current li object to variable
  $('div.other').children().each(function(){ // now iterate through each child div of other div
     if($(this).attr('id') == ultest.data('location')){ // compare id with data-location
      $( this ).css('background-color','red'); // if it matches, add background color
     }
  });
});
#thebirchplot3 {padding:30px;background:#fff;}
#theashplot3 {padding:30px;background:blue;color:#fff;}
#thediveplot1 {padding:30px;background:yellow;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 id="title">Change CSS based on values</h1>
<ul id="test">
    <li data-location="thebirchplot3">one</li>
    <li data-location="theashplot3">two</li>
    <li data-location="thediveplot1">three</li>
</ul>
<ul id="show">
    <li data-location="theashplot3">one</li>
    <li>two</li>
    <li>three</li>
</ul>
<ul id="display">
    <li data-location="thediveplot1">one</li>
    <li>two</li>
    <li>three</li>
</ul>
------
<div>
  <div class="other">
    <div id="thebirchplot3">
    this is dummy
    </div>
    <div id="thebirchplot355435">
    text
    </div>
    <div id="theashplot3">
    which shows you
    </div>
    <div id="theashplot35465464">
    that everything
    </div>
    <div id="thediveplot1">
    working fine now
    </div>
  </div>
</div>

Answer №2

To meet your requirements, utilize the jquery find() method in this way:

var value = $('ul#test').find('li').attr('data-location');

$('div.other').find('#'+value).css('background-color','red');

You can view a working demonstration on this jsFiddle link

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

Implement the Chosen jQuery plugin within an MVC Asp Net framework for enhanced functionality

I want to incorporate the Chosen JavaScript jQuery plugin into an AspNet project. I have completed the following steps: 1- In the Scripts directory, I included all the necessary JavaScript files as shown in the image below. 2- In the Index.cshtml file, I ...

Utilizing jQuery for toggling the visibility of a menu

I have created a code for a navigation page that I am currently developing for use on the Tumblr platform. The navigation setup involves using #navstart as the category of the navigation items and #navitem for individual items. Below is an example of how I ...

What is the maximum string length allowed for the parameter accepted by JavaScript's JSON.Parse() function?

Is there a maximum string length limit for the parameter accepted by JavaScript's JSON.Parse()? If I were to pass a string that surpasses this expected length, will it result in an exception being thrown or prevent the function from returning a valid ...

Trouble with z-index when attempting to stack forms on top of one another

Currently, I am working on creating a login page and signup page for my program. The goal is to stack them on top of each other with only one being displayed at a time. However, I am facing an issue where they are not stacking properly. I attempted using t ...

The function history.popstate seems to be malfunctioning, as it is triggered by both the forward and backward navigation buttons in

When I press the back button, I am attempting to retrieve the previous state. Upon inspecting, I noticed that the popstate function is also triggered by the forward button. However, it does not revert to the previous state even though the popstate function ...

Unable to disable background color for droppable element

For my project, I am working with two div boxes. My goal is to make it so that when I drag and drop box002 into another div box001, the background color of box001 should change to none. I have attempted to achieve this functionality using jQuery without s ...

Dropping challenging shapes in a block-matching game similar to Tetris

I'm currently working on a game similar to Tetris, but with a twist. Instead of removing just one line when it's full, I want to remove all connected pieces at once. However, I've run into a roadblock when trying to implement the hard-drop f ...

I have written code in JavaScript, but now I am interested in converting it to jQuery

What is the best way to convert this to jQuery? showDish("balkandish1") function showDish(dishName) { var i; $(".city").css('display', 'none'); $("#" + dishName).css('display', 'block'); } ...

What techniques can I use to modify an object while it's being iterated through?

I've been attempting to manipulate the object while looping through it, but unfortunately, it's not working. How can I modify it so that the constant patient includes the property lastActivity inside the this.model array? My code looks like this ...

After the state loads in Ui-router, how can I execute a function?

Transitioning from jQuery to Angular, I am struggling with ui-router and states. In jQuery, I can make an AJAX call and then run another function on success. How can this be achieved in Angular? For instance, consider the code snippet below: var ivApp = ...

Unable to save information from Datatable to the MySQL database due to an error

//ADDING NEW ITEM INTO THE ITEM TABLE $(document).on('submit', '#product_form', function(event){ event.preventDefault(); btn_action="add_pricelvl"; //Setting variable to add a new item var valdata = $(this).serialize(); // ...

Unable to adjust the size of the icons sourced from https://github.com/erikflowers/weather-icons

While integrating openweathermap with these icons, I encountered an issue where I couldn't adjust the size of the icons. They are used similarly to font-awesome icons, like this: <i class="wi wi-omw-100"></i> I have tried changing various ...

Find your favorite artist on Spotify through the search function

Recently, I stumbled upon this intriguing demo showcasing how to search for an artist using the Spotify API. However, despite several attempts, I have been unable to make it function properly. Can anyone provide any tips or guidance on making this work suc ...

Revise the JSON format to be compatible with a JavaScript template library

My goal is to utilize JSON data for parsing a template using JavaScript. The Mustache library provides a solution as shown below: var output = $("#output"); // template stored in JS var as a string var templateString = '<div>'+ '< ...

Make sure all of the inner tags are properly contained within their respective containers

In my explanatory bubble, I aim to include text within the explain-header div as a container for my explanations: /*Explain Bubble*/ .explain-container { position: relative; overflow: hidden; max-width: 70vw; max-height: 50vh; background-c ...

Personalized Angular dropdown menu

Recently, I've started delving into angularJS and I'm eager to create dropdowns and tabs using both bootstrap and angular. Although there is a comprehensive angular bootstrap library available, I prefer not to use it in order to gain a deeper und ...

Disabling a button after clicking using jQuery

There are occasions when I can inadvertently trigger the submit button twice, resulting in the ajax being triggered twice as well. Below is my HTML code: <div class="buttons-area"> <input id="step-two" class="btn btn-primary" type="submit" v ...

Having trouble with your jQuery slideDown menu?

I am facing the exact same issue I had last week. Here is an example of my HTML code: <article class="tickets"> <div class="grid_12 left"> <div class="header"> <i class="ico ...

Maximo Anywhere - Conceal or deactivate the list view entry

I have a scenario where I need to call an event handler on a "list" view control in app.xml during the render event. My goal is to hide or disable records in the "list" view based on a flag value. <list displayPageSize="5" id="WorkExecution.WorkItemsV ...

No CanvasItem spills beyond the edges of the canvas

Currently utilizing smartGWT 4.0.x with Firefox version 21. I am working on incorporating typeAhead search using a third-party jQuery plugin. My approach involved creating a canvas where I overrode the getInnerHTML() method to output the HTML for the sear ...