?
Solved

jQuery: Select element with highest value in attribute

Posted on 2016-09-05
1
Medium Priority
?
113 Views
Last Modified: 2016-09-08
Using jQuery, how can I change the background color of the element with the highest value for the attribute data-x?

In this example, only "Eleven" should be red.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

 $('div').filter(function( index ) {return index;}).css( "background-color", "red" );

});

</script>
</head>
<body>

<div data-x="4">Four</div>
<div data-x="11">Eleven</div>
<div data-x="2">Two</div>
<div data-x="3">Three</div>
<div data-x="5">Five</div>
<div data-x="9">Nine</div>

</body>
</html>

Open in new window

0
Comment
Question by:skij
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41784774
Any particular reason you are still using a transitional DOCTYPE?

You could do something like this
CSS
.maxval {
  background: green;
}

Open in new window

HTML
  <div data-x="4">Four</div>
  <div data-x="11">Eleven</div>
  <div data-x="2">Two</div>
  <div data-x="3">Three</div>
  <div data-x="5">Five</div>
  <div data-x="9">Nine</div>

Open in new window

jQuery
$(function() {
  var max = 0, current = false;
  $('div[data-x]').each(function() {
    var value = $(this).data('x');
    if (value > max) {
      current = $(this);
      max = value;
    }
  });
  current.addClass('maxval');
});

Open in new window

Working sample here
1

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question