html/javascript woes

Soldato
Joined
6 Feb 2004
Posts
20,856
Location
England
this is starting to **** me off now. i have a simple line of html.....

Code:
<input type="button" onclick="document.getElementById('message').innerHTML = 'something';">

that works fine - no problem. but in place of 'something', i want to use

Code:
<div id="red"><p><label>&nbsp;</label>please wait!</p></div>

i suppose it should look something like this....

Code:
<input type="button" onclick="document.getElementById('message').innerHTML = '<div id="red"><p><label>&nbsp;</label>please wait!</p></div>';">

but i've tried all combinations of single/double quotes and escaping and none work. i've got firebug installed and i still can't figure it out. :mad:

someone please save my sanity. :D
 
^no, it's not that. :p

oh well i've done a nasty fix - converted this....

Code:
<div id="red"><p><label>&nbsp;</label>please wait!</p></div>

through the php htmlentities function which outputs this....

Code:
&lt;div id=&quot;red&quot;&gt;&lt;p&gt;&lt;label&gt;&amp;nbsp;&lt;/label&gt;please wait!&lt;/p&gt;&lt;/div&gt;

substituted my text with this and it works. :)
 
You could have used a variable which would make it a lot cleaner:

Code:
<script type="text/javascript">
var msg = "<div id=\"red\"><p><label>&nbsp;</label>please wait!</p></div>";
</script>
<input type="button" onclick="document.getElementById('message').innerHTML=msg;">
 
Last edited:
Its simple - you are using quotes / apostrophes inside each other. Pick which one you want to use (I suggest " quotes) and \ all the other ones. ie \" will be interpreted as a literal rather than as the escape from onclick definition.

Demo :

<input type="button" onclick="document.getElementById('message').innerHTML = '<div id=\"red\"><p><label>&nbsp;</label>please wait!</p></div>';">
 
obviously my example above was wrong - i did try lots of combinations of single/double quotes and escaping - the fact it was part of a php echo statement probably wasn't helping things. :p

i settled on the javascript function in the end. this is because i ended up with multiple buttons on the same page so it makes sense. :)

cheers all for the replies.
 
Back
Top Bottom