Answer the question
In order to leave comments, you need to log in
Get the maximum value of an attribute?
Given the following DOM structure:
<div class="tags">
<span id="tag_4" class="tag">tagname</span>
<span id="tag_1" class="tag">tagname</span>
<span id="tag_6" class="tag">tagname</span>
<span id="tag_3" class="tag">tagname</span>
<span id="tag_15" class="tag">tagname</span>
<span id="tag_8" class="tag">tagname</span>
</div>
<div class="tags">
Answer the question
In order to leave comments, you need to log in
tags=document.getElementsByClassName('tags');
for (x in tags){
for (y in tags[x].childNodes){
tags[x].childNodes[y].id.... ну а дальше разберетесь
}
}
Are you using jQuery?
If yes, then:
var maxid = 1;
$(".tags").children().each(function () {
maxid = Math.max(maxid, parseInt($(this).attr("id").replace(/[^\d]/g, ""))+1)
});
tags = document.querySelectorAll('span[id^=tag]');
var n =1;
for(var i in tags){
var n2 = Number(tags[i].id.replace(\tag_/,'') );
if(n2) > n)
n = n2;
}
why not use Math.max?
var a = [2,3,1,10,25,2,4,7], maxValue = Math.max.apply(Math, a);
alert(maxValue)
var oneElementOfArray = Numbser(span.id.substr(4)) || 0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question