How would I use JS to get this value?

L

laredotornado

Hi,

On my HTML page, I have a number of SPANs like

<span class="myNumber">3</span>

What I would like is a way to return the maximum number in between the
SPANs. You can assume that if I have an HTML block like the above, I
will also have

<span class="prescriptionNumber">1</span>
<span class="prescriptionNumber">2</span>

somewhere on my page before it.

Thanks for your help, - Dave
 
E

Evertjan.

laredotornado wrote on 03 aug 2008 in comp.lang.javascript:
Hi,

On my HTML page, I have a number of SPANs like

<span class="myNumber">3</span>

What I would like is a way to return the maximum number in between the
SPANs. You can assume that if I have an HTML block like the above, I
will also have

<span class="prescriptionNumber">1</span>
<span class="prescriptionNumber">2</span>


"returning the maximum number in between the SPANs."

What is a "maximum number in between"?
somewhere on my page before it.

Before what it?
 
G

Gregor Kofler

laredotornado meinte:
Hi,

On my HTML page, I have a number of SPANs like

<span class="myNumber">3</span>

What I would like is a way to return the maximum number in between the
SPANs. You can assume that if I have an HTML block like the above, I
will also have

<span class="prescriptionNumber">1</span>
<span class="prescriptionNumber">2</span>

somewhere on my page before it.

(1) Get collection of all spans with className "prescription".
(2) Collect the first childNode of all those spans.
(3) convert the nodeValues of those childNodes to numbers (if needed).
(4) search highest value of those numbers.

Gregor
 
L

laredotornado

laredotornadomeinte:







(1) Get collection of all spans with className "prescription".
(2) Collect the first childNode of all those spans.
(3) convert the nodeValues of those childNodes to numbers (if needed).
(4) search highest value of those numbers.

Gregor

--http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com     ::: Bildagentur für den alpinen Raum

Thanks. How do I 'Get collection of all spans with className
"prescription"'? - Dave
 
T

Thomas 'PointedEars' Lahn

laredotornado said:
laredotornadomeinte:
On my HTML page, I have a number of SPANs like
<span class="myNumber">3</span>
What I would like is a way to return the maximum number in between the
SPANs. You can assume that if I have an HTML block like the above, I
will also have
<span class="prescriptionNumber">1</span>
<span class="prescriptionNumber">2</span>
somewhere on my page before it.
(1) Get collection of all spans with className "prescription".
(2) Collect the first childNode of all those spans.
(3) convert the nodeValues of those childNodes to numbers (if needed).
(4) search highest value of those numbers.
[...]

Thanks. How do I 'Get collection of all spans with className
"prescription"'?

Google is your friend.


PointedEars
 
L

Lasse Reichstein Nielsen

laredotornado said:
Thanks. How do I 'Get collection of all spans with className
"prescription"'?

There is no single method that does that, so you have to write a
function yourself.
In this case, since you are only looking for "span" elements,
you could use document.getElementsByTagName to get all span
elements in the document, and then run through that and operate
on those with a "className" property containing "prescription".

E.g.
function getPrescriptions() {
var ps = [];
var spans = document.body.getElementsByTagName("span");
for(var i = 0, n = spans.length; i < n; i++) {
var sp = spans;
if (/\bprescription\b/.test(sp.className)) {
ps.push(sp);
}
}
return ps;
}

Good luck
/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,814
Members
47,359
Latest member
Claim Bitcoin Earnings. $

Latest Threads

Top