3

I need to find minimum and maximum value in data attribute but it returns 0

here is my code

<div data-price="2" class="maindiv">test</div>
<div data-price="5" class="maindiv">test</div>
<div data-price="3" class="maindiv">test</div>
<div data-price="0" class="maindiv">test</div> 
<div data-price="25" class="maindiv">test</div>     
<div data-price="10" class="maindiv">test</div> 

<script>
    var ids = $("div[data-price]").map(function() {
        return this.id;
    }).get();

    var highest = Math.max.apply( Math, ids );
    var lowest = Math.min.apply( Math, ids );

    alert(highest);
    alert(lowest);
</script>
1
  • Did these answers provide solution to your problem? Commented Jun 13, 2016 at 10:57

3 Answers 3

7

You need to return data-price attribute, not its id

var ids = $("div[data-price]").map(function() {
    return $(this).attr("data-price");
}).get();

var highest = Math.max.apply( Math, ids );
var lowest = Math.min.apply( Math, ids );

alert(highest);
alert(lowest);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-price="2" class="maindiv">test</div>
<div data-price="5" class="maindiv">test</div>
<div data-price="3" class="maindiv">test</div>
<div data-price="0" class="maindiv">test</div> 
<div data-price="25"class="maindiv">test</div>     
<div data-price="10" class="maindiv">test</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this with regular JavaScript:

//Variable to hold our results
var data = [];
//Loop through HTML nodes
for (var i = 0; i < document.querySelectorAll(".maindiv").length; i++) {
	data.push({
		node : document.querySelectorAll(".maindiv")[i],
		value : parseInt(document.querySelectorAll(".maindiv")[i].getAttribute("data-price"))
	});
}
//Sort lowest to highest (Only use one sort! Pick either this or the next one as they overwrite each Other)
data = data.sort(function (a, b) {
		return a.value - b.value;
	});
//Sort highest to lowest (Only use one sort! Pick either this or the last one as they overwrite each Other)
data = data.sort(function (a, b) {
		return b.value - a.value;
	});
//List all
console.log(data);
//List first (highest or lowest depending or sort)
console.log('first', data[0].value);
//List last (highest or lowest depending or sort)
console.log('last', data[data.length-1].value);
<div data-price="2" class="maindiv">test</div>
<div data-price="5" class="maindiv">test</div>
<div data-price="3" class="maindiv">test</div>
<div data-price="0" class="maindiv">test</div> 
<div data-price="25" class="maindiv">test</div>     
<div data-price="10" class="maindiv">test</div>

Comments

0

Currently you are returning the id attribute from the map function. Change it to to data-price

var ids = $("div[data-price]").map(function() {
  return $(this).data("price");
}).get();
var highest = Math.max.apply( Math, ids );
var lowest = Math.min.apply( Math, ids );
alert(highest)
alert(lowest)

Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.