0

Wordpress has a Text Widget (at least the theme I'm using does, and I think they all do?)

In this widget I can add text or HTML. If I add HTML I can include JavaScript. This works fine (I'm hosting my own site).

I want to add some text to the Widget using a script. However;

document.write("Whatever")

writes the text onto the actual web page.

What command do I use to write text into the Text Widget only.

Many thanks for any help!

1
  • Please be a bit more specific. Firstly, where are you putting this JavaScript? In the widget itself, or in a separate file, or in the header or something? And secondly, are you trying to APPEND text to the widget (add text on top of what's already there), or replace the entire contents of the widget?
    – MathSmath
    Jan 12, 2011 at 15:57

1 Answer 1

0

Where are you using document.write? Inside the text widget? If so, it should write text inside that widget, just like you want.

document.write injects data where it's encountered. So if you're using it outside the document body it will produce unexpected results (like overwrite the document, in most cases).

**Update, add this in your text widget:

 <div id="textwidget-content"> YOUR INITIAL TEXT HERE </div>
 <script type="text/javascript">
   var strings = new Array('Whatever', 'Bla blah', 'Boo', '....');
   var t = setInterval(function(){
     document.getElementById('textwidget-content').innerHTML = strings[Math.floor(Math.random()*(strings.length))];}, 1000);
 </script>
4
  • Thanks very much for the answer. Yes, I'm using document.write inside the text widget. But I can't write the text directly inside the widget, because I want the text to change (cycle through an array of strings, say 1 per second). I don't know what the div id of the text widget is. That's really a major part of my problem. How can I find that out? Many thanks again for helping out so far.
    – Ariel
    Jan 12, 2011 at 16:03
  • @MathSmith: I didn't see your comment before. I'm putting the JavaScript in the widget itself (creating a header, and calling the script from the body), just as though I were writing text, i.e., using the regular wordpress admin dashboard to copy and paste some HTML into the widget. The HTML I'm pasting into the widget includes a header and a body. The script is in the header. I'm trying to replace the contents of the widget every second.
    – Ariel
    Jan 12, 2011 at 16:27
  • there's no such thing as header/body tag inside a widget. Try my example above, it should do what you want Jan 12, 2011 at 17:05
  • Bravo!! Thanks so much! Creating a new div is obviously the simplest way! You've really helped me. I ticked the V mark, hope that gives you points or whatever.
    – Ariel
    Jan 12, 2011 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.