SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2005
    Location
    Hull
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOM Scripting: Using a value from a select field

    Hi there,

    I am trying to use a value from the select field to replace text within a strong tage;

    Code:
    <strong id="selected_country">Country Will appear here</strong>
    So far the script anounces the selected country using alert. Here is the scrupt so far:
    Code:
    function getCountry() 
    { 
     // Validate that the browser knows getElementById 
     if(!document.getElementById ) return; 
     // Get the "pages" select box 
     var pagesObj = document.getElementById("distributor");
     // Validate that the object exists 
     if(!pagesObj ) return;  
     // Get the options array. This works for both 
     // single select and multi select boxes 
     if( typeof(pagesObj.options) == "undefined" || pagesObj.options == null ) return; 
     var o = pagesObj.options; 
     // Get the selected options into an array 
     var s = new Array(); 
     for( var i = 0; i < o.length; i++ ) 
     { 
      if( o[i].selected ) 
      { 
       s[s.length] = o[i].value; 
      } 
     } 
     // s now contains an array of the selected values. 
     // If nothing was selected, the array length is 0 
     if( s.length == 0 ) 
     { 
      alert("You must select a value."); 
     } 
     else 
     { 
      for( var i = 0; i < s.length; i++ ) 
      { 
       // Do whatever you want with the values here 
       var selectedCountry =  s[i];
    //HERE IS WHERE I THINK IT GOES
    alert(selectedCountry)
    }
       
      }
     }
    }
    Any help would be a bonus.

    Thanks
    Darren

  2. #2
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing innerHTML. I'm not sure if it would work though.

    selectedCountry.innerHTML = 'some html code';

  3. #3
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.getElementById("selected_country").innerHTML = selectedCountry;


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
  •