/    Sign up×
Community /Pin to ProfileBookmark

counting id attributes

Hi

Javascript newbie here – I know my way around a bit of serverside and SQL stuff but client side still eludes me slightly.

On a page, I have several div tags with incrementally numbered id attributes:

[code]
<div id=”content[1]”>blah blah</div>
<div id=”content[2]”>yada yada</div>
<div id=”content[3]”>rhubarb rhubarb</div>
[/code]

What I need is a function that can count the number of ‘content’ div tags – i.e. how large the content array is.

I’ve taken a pre-written script and jigged with it so it does what I need but currently I need to manually update a variable holding how many content div tags there are which is an ugly solution.

Can anyone help me out please? ? Thanks in advance!

P.S. This is my script as it currently stands. Any advice is welcomed ?:

[code]
function DS_switchto(id)
{
var numberofpages=2;
if (document.all)
{
for (z = 1; z <= numberofpages; z++)
{
document.all[‘content[‘+z+’]’].style.display = ‘none’;
}
document.all[‘content[‘+id+’]’].style.display = ”;
return false;
} else if (document.getElementById)
{
for (z = 1; z <= numberofpages; z++)
{
document.getElementById[‘content[‘+z+’]’].style.display = ‘none’;
}
document.getElementById(‘content[‘+id+’]’).style.display = ‘block’;
return false;
}
}
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@AdamGundryFeb 27.2005 — Perhaps something like this...

function switch2(id){
divs = document.getElementsByTagName('div');
for (var i=0; i&lt;divs.length; i++)
if (divs[i].id.substr(0, 7) == 'content')
divs[i].style.display = (divs[i].id.substr(8, 1) == id ? 'block' : 'none');
}

Adam
Copy linkTweet thisAlerts:
@solomonauthorFeb 27.2005 — Excellent! that works a treat - thanks Adam.

I was only expecting a system to count the div tags, but you clearly can't bear to see clumsy scripting ?
×

Success!

Help @solomon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...