Newbie : Problem changing style on named objects.

P

Paul Blay

Unfortunately what javascript I did know I've mostly forgotten.

Basically I have something like this

<p>[first half of sentence]
<ruby>
<rb>
<span name="k_1123" id="k_1123" style="display:none">[answer]</span>
<span style='display:inline' id="s_1123" name="s_1123">XXXX</span>
</rb>
<rp>(</rp>
<rt>[hint]</rt>
<rp>)</rp>
</ruby>[second half of sentence]<br/>
<input type=button value="Check!" name="b_1123" onclick=
"document.getElementsByName('k_1123').style.display='inline';document.getElementsByName
('s_1123').style.display='none';"/>
</p>

On clicking the 'Check!' button I want the 'XXXX' hidden and the '[answer]' shown.

Except that what I get is
'Error: document.getElementsByName("k_1123").style has no properties'
Which I assume means I'm doing something stupid.

I've tried various things out and looked through a fair number of web-pages
but I'm officially giving up and asking for a pointer in the right direction.
 
M

Martin Honnen

Paul said:
Unfortunately what javascript I did know I've mostly forgotten.

Basically I have something like this

<p>[first half of sentence]
<ruby>
<rb>
<span name="k_1123" id="k_1123" style="display:none">[answer]</span>

Throw out those name attributes and use id attributes. Then you can use
var el;
if (document.getElementById) {
el = document.getElementById('k_1123');
if (el && el.style) {
el.style.display = 'inline';
}
}
Forget about getElementsByName, it is less supported than
getElementById, it is supposed to return a collection and which elements
take name attributes and are found by the method is browser dependant.

You might also want to avoid the underline _ in id and class attribute
values as some browsers have difficulties with that:
http://devedge.netscape.com/viewsource/2001/css-underscores/
 
P

Paul Blay

Forget about getElementsByName, it is less supported than
getElementById, it is supposed to return a collection and which elements
take name attributes and are found by the method is browser dependant.

Brilliant! (That's just the sort of thing I might have spent all day at and not
noticed)

And thanks for the tip on _ as well.
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top