0 Comments

To set the text on a control with javascript use:

 

/*
    Set the text value of the given element.
    This function assumes this document is defined and not null and has a function getElementById
    Used by: -
*/
function SetText(elementId, text)
{
    // Check input values
    if(IsNullOrUndefined(document)){alert('SetText: Object [document] is null or undefined');}
    if(IsNullOrUndefined(elementId)){alert('SetText: Object [elementId] is null or undefined');}
    if(IsNullOrUndefined(text)){text = '';}

    // Get the given element by id
    var resultGetElementById = document.getElementById(elementId);
    if(IsNullOrUndefined(resultGetElementById)){alert('SetText: Object [resultGetElementById] is null or undefined');}

    // Check if the firstChild is null or undefined
    if(IsNullOrUndefined(resultGetElementById.firstChild))
    {
        // Element does not have a first node, add an textnode.
        resultGetElementById.appendChild(document.createTextNode(text));
    }
    else
    {
        // Element has a first childe node, so replace it's text value.
        resultGetElementById.firstChild.nodeValue = text;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts