CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2006
    Posts
    326

    use combo box change color.

    Code:
    var authors = new Array(794);
    authors[0] = 'SnakeBot';
    authors[1] = 'SnakeBot';
    authors[2] = 'SnakeBot';
    authors[3] = 'Tdvance';
    authors[4] = 'SnakeBot';
    authors[5] = 'ClueBot';
    authors[6] = 'SnakeBot';
    authors[7] = 'SnakeBot';
    .....
    authors[789] = 'SnakeBot';
    authors[790] = 'SnakeBot';
    authors[791] = 'SnakeBot';
    authors[792] = 'SnakeBot';
    authors[793] = 'SnakeBot';
    var authorList = new Array(15);
    authorList[0] = 'Ckatz';
    authorList[1] = 'Mqrasi';
    authorList[2] = 'Geedubs';
    authorList[3] = 'PipepBot';
    authorList[4] = 'DumbBOT';
    authorList[5] = 'Cosprings';
    authorList[6] = 'Formation';
    authorList[7] = 'ClueBot';
    authorList[8] = '70.21.43.33';
    authorList[9] = 'Tdvance';
    authorList[10] = 'SnakeBot';
    authorList[11] = 'KocjoBot';
    authorList[12] = 'Ohnoitsjamie';
    authorList[13] = 'Ackers2210';
    authorList[14] = 'KnowledgeOfSelf';
    function show0(value) {
        var x=parseInt(value);
        for (var i=0;i<793;++i) {
       var obj = document.getElementById("wiki"+i);
       if (obj && dur[i] <=x) 
            obj.style.backgroundColor = "red";
       else if (obj && dur[i] >x) {
           obj.style.color = "black";
           obj.style.backgroundColor = "pink";
       }
     }
    }
    function show1(value) {
        var x=parseInt(value);
        for (var i=0;i<793;++i) {
           var obj = document.getElementById("wiki"+i);
           if (obj && dur[i] >x) 
              obj.style.backgroundColor = "green";
           else if (obj && dur[i] <=x) {
              obj.style.color = "black";
           obj.style.backgroundColor = "yellow";
          }
        }
    }
    function start_this() {
       if(history.previous != history.current) {
           windows.location.reload(true);
         }
    
      for (var i=0;i<15;i++) {
       var x = document.createElement("option");
       var y = document.createTextNode(authorList[i]);
       x.appendChild(y);
       var z = document.getElementById("myauthorlist");
       z.appendChild(x);
      }
      }
    </script>
    </head>
    <body onload="start_this()" class="mediawiki ns-0 ltr page-Music">
    <form>(Display with red) Less than: <input type='text' id='req1' />
    <input type='button' onclick='show0(document.getElementById("req1").value)' value='days' />
    </form>
    <form> (Display with green) Great than: <input type='text' id='req2' />
    <input type='button' onclick='show1(document.getElementById("req2").value)' value='days' />
    </form>
    <br></br>
    <p>Select the author:</p>
    <p>
    <select name = "name" size = 1 id = "myauthorlist">
    <br></br>
    <div id="wiki615" id="globalWrapper">
    <div id="wiki604" id="column-content">
    <div id="wiki624" id="content">
    <a id="wiki245" name="top" id="top"></a>
    ....
    I want to select an item from the box then the change the displaying color.
    For ex, once I click 'SnakeBot',since author[0] = 'SnakeBot', therefore the lines with
    id="wiki0" will be displayed as "yellow".
    Do I have to apply any event?
    Last edited by PeejAvery; November 16th, 2007 at 08:53 AM. Reason: Fixed confusing double post.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: use combo box change color.

    You can't have more than 1 ID in an HTML tag.

    Code:
    <div id="wiki615" id="globalWrapper">
    <div id="wiki604" id="column-content">
    <div id="wiki624" id="content">
    Since you can use a variable to read through the authors array, just use that same variable to call the wiki ID.

    Code:
    document.getElementById('wiki' + i).style.backgroundColor = '#ffff00';
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jan 2006
    Posts
    326

    Re: use combo box change color.

    Ignore the multiple id.

    Acturaly my question is how to retrieve a value that is selected from a combobox.
    Code:
    function start_this() {
       if(history.previous != history.current) {
           windows.location.reload(true);
         }
       var comboValue;
       var z = document.getElementById("myauthorlist");
       for (var i=0;i<15;i++) {
           var x = document.createElement("option");
           x.setAttribute('value',authorList[c]);
           var y = document.createTextNode(authorList[i]);
           x.appendChild(y);
           z.appendChild(x);
      }
      comboValue = document.myauthorlist.value;
    // use comboValue do something
    }
    </script>
    </head>
    <body onload="start_this()">
    <p>Select the author:</p>
    <select name = "name" size = 1 id = "myauthorlist">
    </select>

  4. #4
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: use combo box change color.

    The multiple ID shouldn't be ignored if you're using it anywhere (such as in CSS or JavaScript) because it can cause unexpected behavior on your page.

    Anyways, to get the currently selected option in a list, you'd use this:
    Code:
    var select = document.getElementById('myauthorlist');
    var selectedIndex = select.selectedIndex;
    It's zero-based (the first option has index 0) and will have the value -1 if nothing is selected (only really relevant if you're displaying it as a list rather than a combo box.)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured