View Full Version : Use of variables

07-21-2010, 01:54 AM
Noob here. Please bear with me.http://www.codingforums.com/images/smilies/tongue.gif

<span title="this is the title">....</span>

I need to use variables for the title instad of text.

Please advise. Thank you.

Old Pedant
07-21-2010, 02:43 AM
<script type="text/javascript">
var sptitle = "This is title number " + (++counter);
var spcontents = "Now the number is " + counter;
document.write('<span title="' + sptitle + '">' + spcontents + '</span>');

Is something like that what you are talking about??

You could also do it via:

<span id="whatsit">whatever</span>
<script type="text/javascript">
document.getElementById("whatsit").title = "tick " + Math.random() + " tock";

07-21-2010, 02:55 AM
Thanks OldPedant.
Better the first option. It keeps the <span> unique.

Old Pedant
07-21-2010, 03:10 AM
It keeps the <span> unique.

??? No more so than the second one. Either set of code could produce the results of the other set. It was all made-up arbitrary code.