Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.write with onClick

Status
Not open for further replies.

The

Programmer
Jul 30, 2001
92
CA
I want a script where when you click different links, it changes certain text. I'm doing it like this:

<a href=&quot;blah.html&quot; onClick=&quot;document.write('Blah');&quot;>Blah</a>

My only problem is, instead of writing &quot;blah&quot; on the existing page, it goes to a blank page with &quot;blah&quot; written on it. How can I fix this? Is there something better than document.write that I could use? Thanks.
 
By referencing a page in the href attribute of the A element - in this case &quot;blah.html&quot; - you're telling your browser to go to &quot;blah.html&quot;, and with the onClick you're telling it to write &quot;Blah&quot; when it gets there. What you want to do is change text on the existing page without reloading it - for that, you'll probably want to ignore document.write altogether, and use the innerText or innerHTML methods in a JavaScript function. It depends on where on the page you're writing the values depicted in the links. Something like this:

<SCRIPT language=&quot;JavaScript&quot;>
function WriteText(target,newText){
var oTarget = document.getElementById(target) //the object that will be holding your text
var oldText = oTarget.innerText;
var retVal;

if (newText != '') {
oldText = oldText + ' ' + newText;
oTarget.innerText = oldText;
retVal = 0
} else {
retVal = 1
}
}
</SCRIPT>

Then, in your link code, change it to look like:

<a href=&quot;javascript:WriteText('targetArea','Blah')&quot; onMouseOver=&quot;window.status='';return true&quot; onMouseOut=&quot;window.status='';return true&quot; onClick=&quot;window.status='';return true&quot;>Blah</a>

Providing the &quot;window.status='';return true&quot; in the onMouseOver, onMouseOut and onClick events of the A element hides the JavaScript function call from the browser's status bar.

HTH,
jp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top