SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing a text onChange

    Hello,

    I have a code like this:

    Code:
    <select name="option">
    <option value="choice1">Choice1</option>
    <option value="choice2">Choice2</option>
    </select>	 
    <p>Bla bla bla bla....</p>
    What I want to do is display a certain text between the <p>-tags when visitors select "choice1" and display different text when visitors select "choice2". I know that this needs to be done with onChange() and document.getElementById(), but I don't exactly know how to do it.
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Code HTML4Strict:
    <select id="option" name="option">
    <option value="choice1">Choice1</option>
    <option value="choice2">Choice2</option>
    </select>


    Code JavaScript:
    // attach function to select element
    var select = document.getElementById('option');
    select.onchange = updateParagraph;
     
    function updateParagraph() {
        // set text value
        var text = '';
        if (this.value === 'choice1') {
            text = 'Text 1';
        } else if (this.value === 'choice2') {
            text = 'Text 2';
        }
     
        // set paragraph
        if (!(document.getElementById('selectPara'))) {
            // add select paragraph
            var p = document.createElement('p');
            p.id = 'selectPara';
            if (this.nextSibling) {
    			var next = this.nextSibling;
                // add paragraph before the next sibling
                next.parentNode.insertBefore(p, next);
            } else {
                // add paragraph after select element
                this.parentNode.appendChild(p);
            }
        }
     
        // clear paragraph
        var para = document.getElementById('selectPara');
        while (para.firstChild) {
            para.removeChild(para.firstChild);
        }
     
        // add text to paragraph
        para.appendChild(document.createTextNode(text));
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Code HTML4Strict:
    <select id="option" name="option">
    <option value="choice1">Choice1</option>
    <option value="choice2">Choice2</option>
    </select>


    Code JavaScript:
    // attach function to select element
    var select = document.getElementById('option');
    select.onchange = updateParagraph;
     
    function updateParagraph() {
        // set text value
        var text = '';
        if (this.value === 'choice1') {
            text = 'Text 1';
        } else if (this.value === 'choice2') {
            text = 'Text 2';
        }
     
        // set paragraph
        if (!(document.getElementById('selectPara'))) {
            // add select paragraph
            var p = document.createElement('p');
            p.id = 'selectPara';
            if (this.nextSibling) {
    			var next = this.nextSibling;
                // add paragraph before the next sibling
                next.parentNode.insertBefore(p, next);
            } else {
                // add paragraph after select element
                this.parentNode.appendChild(p);
            }
        }
     
        // clear paragraph
        var para = document.getElementById('selectPara');
        while (para.firstChild) {
            para.removeChild(para.firstChild);
        }
     
        // add text to paragraph
        para.appendChild(document.createTextNode(text));
    }
    Wow! This seems complicated!!
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The code to set the paragraph can be removed if you already have the empty paragraph on the page. It is less desired though because that empty paragraph will still be there when nothing has been selected, and if javascript isn't supported by the visitor.

    When someone choses a second option the paragraph needs to be cleared, before it is updated, so that really has to stay, and the rest is pretty much compulsory as well.

    Here's how short it can be with comments and other pieces reduced.

    Code Javascript:
    document.getElementById('option').onchange = function () {
        var t = (this.value === 'choice1') ? 'Text 1' : 'Text 2';
        var p = document.getElementById('selectPara');
        while (p.firstChild) {p.removeChild(p.firstChild);}
        p.appendChild(document.createTextNode(t));
    };

    As you can see, the bulk of the original code makes it easier to understand, and also to make changes to it.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •