Run.sayWhat() has two jobs- to return the text content of an element, or to
replace the text with something else.
| index | required | description | type |
|---|---|---|---|
| [0] | yes | an html element | string or object |
| [1] | no | text replacement string | string |
| [2] | no | new style properties | string |
Run.sayWhat() is a simple guy to use. The first argument is a string id or object reference to an element in the document. The return value is the text content of the element. An optional second argument is a string that is used to replace the text. And a third argument can be passed to change the displayed style of the element.
Say we have an h2 element with the id 'h2_5':
Run.sayWhat('h2_5') returns 'Heading Example' and doesn't change anything.
(Run.sayWhat('h2_5') == 'Heading Example')? Run.mrs('h2_5',
'display:block') :
Run.mrs('h2_5','display:none');
The previous bit will display the heading if it matches our text, and hide it otherwise. See Run.mrs(hoo) to see how the style is applied, if you are curious. You don't need to use Run.mrs(hoo):
if(document.getElementById('h2_5') &&
Run.sayWhat('h2_5') == 'Heading Example')?
document.getElementById('h2_5').style.display= 'block' :
document.getElementById('h2_5').style.display= 'none' ;
}
And to replace the text:
Run.sayWhat('h2_5', 'New Example') returns 'Heading Example' and changes the heading:
And if we want to change the text and the style:
Run.sayWhat('h2_5', 'Third Example', 'color:#ff0000') returns 'New Example', and displays:
if( !mr(hoo)) return;
var who= mr(hoo);
var hasText= false;
var wotText= '';
/* Use mr(hoo) to identify the element we want. Initialize local variables. */
sayWotLoop:
/* Label the outer loop so we can break out of it. */
if(who.hasChildNodes()){
for(var i=0; i< who.childNodes.length; i++){
/* loop through the childNodes of the element. The DOM method normalize should work to concatenate adjacent text nodes, but it has some funky behavior in one of our favorite browsers, so I do it the long way. */
if(who.childNodes[i].nodeType== 3){
/* If this child is a text node do your stuff. */
hasText= true;
wotText+= who.childNodes[i].data;
/* Let the function know there is text in the node, and add the text to wotText. If we are just querying for text, keep looping and add all the text in all the children to the return value. If we are changing the text, do it next. */
if(txt !=null){
who.childNodes[i].data= txt;
break sayWotLoop;
/* If we passed a string of text in the second argument, change the first child that has text to hold our replacement text. Then break out of the loop. */
}
}
}
}
if(hasText==false && txt != null)
who.appendChild(document.createTextNode(txt));
/* If the element has no text and we want to give it some, use the createTextNode() method to do so. Be careful here- don't try to add text to an element that can't use it, like an image or a br or hr. If this is a problem for you, wrap this bit in a try loop and return it true from the catch statement. */
if( sty!= null ) mrs(who, sty);
/* This is a handy option. A lot of times you want to change the style of an element when you change the text. This calls Run.mrs(hoo) to take care of it for you, though you can change the style in 'longhand', if you prefer. */
return wotText;
/* The return value is the text content of the element before any replacement. */
}