SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    appending a span element

    From a radio group, when the radio button with the word "correct" as part of the class attribute value is clicked I want this function to append the text "Correct!" next to the radio input control that was clicked. But the following function does not write anything anywhere. Why?

    Code JavaScript:
    function displayCorrectText(target){
     
    	//target is the radio input control that was clicked in the current form
     
    	var newSpan = document.createElement("span");
    	var spanText = document.createTextNode("Correct!");
     
    	newSpan.appendChild(spanText);
    	newSpan.className = "correct";
     
     
     
    	target.appendChild(newSpan);
     
    }

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An INPUT element cannot contain another element. It's what is called a "closed tag". You need to append the SPAN to the element that contains the INPUT.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    To add an element on to the end of another one takes the following code:

    Code javascript:
    if (target.nextSibling) {
        target.parentNode.insertBefore(newSpan, target.nextSibling);
    } else {
        target.parentNode.appendChild(newSpan);
    }
    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
  •