spaces.. how to deal with this?

J

J. Franchino

Why doesn't this work? specifically, the space in "word word" throws
it all off. What's the right way to do this? Thanks. -jf

<script language="JavaScript">
function change_text(id,str) {
document.getElementById(id).innerHTML=str
}
</script>

<div onclick=change_text("num1","word word")>click here</div>


Span: <b><span id="num1">num1</span></b>
 
R

RobG

J. Franchino said:
Why doesn't this work? specifically, the space in "word word" throws
it all off. What's the right way to do this? Thanks. -jf

<script language="JavaScript">

The language attribute is depreciated, type is required:

function change_text(id,str) {
document.getElementById(id).innerHTML=str
}
</script>

<div onclick=change_text("num1","word word")>click here</div>

While HTML element attribute values don't always need quotes, onclick
attribute values nearly always do. It's good practice to always quote
all attribute values.

Change the above to:

<div onclick="change_text('num1','word word');">click here</div>

Note the use of single quotes inside double quotes (you could also use
doubles inside singles...) but nest any deeper and you need to start
quoting quotes with backslash character \.

Be cautious with innerHTML, it is a quick 'n dirty method for replacing
HTML. It's behaviour is not standardised and has some significant
differences on various platforms.

[...]
 
A

ASM

J. Franchino said:
Why doesn't this work? specifically, the space in "word word" throws
it all off. What's the right way to do this? Thanks. -jf

<script language="JavaScript">
function change_text(id,str) {
document.getElementById(id).innerHTML=str
}
</script>

<div onclick=change_text("num1","word word")>click here</div>


Span: <b><span id="num1">num1</span></b>

with :

<div onclick="change_text('num1','word word')">click here</div>

it works fine (Mac & FF)
 

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,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top