Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array populate drop down, css format

Status
Not open for further replies.

vasox56

Technical User
May 8, 2007
75
GB
hi.. im using an array to populate my drop down menu.. here is the script im using


Code:
<script type="text/javascript">
  // an array to hold the contents of all lists
  var DataArray = new Array();


DataArray[0] = new Array("C1","EC1 - City, Clerkenwell", "p1");
DataArray[1] = new Array("C1","EC2 - City, Tower", "p2");
DataArray[2] = new Array("C1","EC3 - City, Bank", "p3");
DataArray[3] = new Array("C1","EC4 - City, Holborn", "p4");

DataArray[4] = new Array("C1","WC1 - Bloomsbury", "p5");
DataArray[5] = new Array("C1","WC1 - St Pancras", "p6");
DataArray[6] = new Array("C1","WC2 - Charing Cross", "p7");
DataArray[7] = new Array("C1","WC2 - Covent Garden", "p8");
DataArray[8] = new Array("C1","WC2 - Holborn", "p9");
DataArray[9] = new Array("C1","WC2 - Leicester Square", "p10");


  // swap contents of second list
  function switchList(List1)
  { var i, k;
    var List2 = document.getElementById("SecondList");
    var TypeOfValues = List1.options[List1.selectedIndex].value;

    //Remove all items in SecondList listbox
    //Start removing items at end and work back to front
    k = List2.options.length - 1;
    for (i = k; i >= 0; i--)
    { List2.options[i] = null;
    }

    k = 0;
    for(i = 0; i < DataArray.length; i++)
    { if (DataArray[i][0] == TypeOfValues)
      { List2.options[k] = new Option(DataArray[i][1], DataArray[i][2]);  
        k++;
      }
    }
  }
</script>

in a traditional drop down.. if i wanted one of these items to be formated i could just do

Code:
<option value="p1" style="blabla">EC1 - Central London</option>

if i wanted to add css formating to certain items within my array can this be done directly within the script? thanks
 
Yes it can be done directly within a script. The better idea is to assign classnames to the <options> instead of an inline style.

Then you can use javascript to change the classname.

Assuming you know what a CSS class is, the javascript to assign/change a class is like this (I'll use a snippet of your code):

Code:
    for(i = 0; i < DataArray.length; i++)
    { if (DataArray[i][0] == TypeOfValues)
      { List2.options[k] = new Option(DataArray[i][1], DataArray[i][2]);  
        [!]List2.options[k].className = "differentBackground";[/!] 
        k++;
      }
    }

Where class differentBackground looks like this:
Code:
.differentBackground {
   background-color:#ff0000;
}


[monkey][snake] <.
 
ok.. so if i want to add the css class to one of my array items.. eg this

Code:
DataArray[4] = new Array("C1","WC1 - Bloomsbury", "p5");

can i do it like this?

Code:
DataArray[4] = new Array("C1","WC1 - Bloomsbury", "p5");
DataArray[4].className = "differentBackground";

because where you have placed that line above seems like it is formating a group of things.. rather then a specific one
 
No, class names can only be added to HTML objects, which in your case was an array of <option> tags.

[monkey][snake] <.
 
so are you telling me that the array is just a table of data.. and thats it

am i not able to format specific postcodes within that array...

so that record in the array displays in a customized way in my drop down

this will be very bad if i cant do it.
 
oh no.. what am i supposed to do now then..

i want to format the way the drop down displays the items in the array.. add margins.. titles.. colours.. underlines..

if i cant do this i might have to scrap the array altogether.. this is terrible news.. help me!

can you suggest anything?
 
Do you see my original response?

You CAN apply styles to the HTMl objects that the array information goes into.

You are pulling that array information into a dropdown.

You can style the dropdown BASED on the current value within the array at the time if you loop through the array.

Here is an example of that:

Code:
    for(i = 0; i < DataArray.length; i++)
    { if (DataArray[i][0] == TypeOfValues)
      { List2.options[k] = new Option(DataArray[i][1], DataArray[i][2]);  
        [!]if (DataArray[i][1] == "WC1 - Bloomsbury") { 
           List2.options[k].className = "differentBackground";
        }[/!]   
        k++;
      }
    }

That code will change the background color of an option IF and ONLY IF DataArray[1] = "WC1 - Bloomsbury".

Take the time to try to understand the code.

[monkey][snake] <.
 
yeah.. i think i just have a bit of a problem with the }

im trying this and it isnt working.. what is wrong here..

and also to save time.. if you could in red show me how i can set a rule for another object.. just so i know when to start and end the {}

Code:
<script type="text/javascript">

.difmarg {
   margin-left:5px;
}


  // an array to hold the contents of all lists
  var DataArray = new Array();


DataArray[0] = new Array("C1","EC1 - City, Clerkenwell", "p1");
DataArray[1] = new Array("C1","EC2 - City, Tower", "p2");
DataArray[2] = new Array("C1","EC3 - City, Bank", "p3");
DataArray[3] = new Array("C1","EC4 - City, Holborn", "p4");

DataArray[4] = new Array("C1","WC1 - Bloomsbury", "p5");
DataArray[5] = new Array("C1","WC1 - St Pancras", "p6");
DataArray[6] = new Array("C1","WC2 - Charing Cross", "p7");
DataArray[7] = new Array("C1","WC2 - Covent Garden", "p8");
DataArray[8] = new Array("C1","WC2 - Holborn", "p9");
DataArray[9] = new Array("C1","WC2 - Leicester Square", "p10");




  // swap contents of second list
  function switchList(List1)
  { var i, k;
    var List2 = document.getElementById("SecondList");
    var TypeOfValues = List1.options[List1.selectedIndex].value;

    //Remove all items in SecondList listbox
    //Start removing items at end and work back to front
    k = List2.options.length - 1;
    for (i = k; i >= 0; i--)
    { List2.options[i] = null;
    }

    k = 0;
     for(i = 0; i < DataArray.length; i++)
    { if (DataArray[i][0] == TypeOfValues)
      { List2.options[k] = new Option(DataArray[i][1], DataArray[i][2]);  
        if (DataArray[i][1] == "WC1 - Bloomsbury") {
           List2.options[k].className = "difmarg";
        }   
        k++;
      }
    }
</script>
 
Ok, this code:
Code:
.difmarg {
   margin-left:5px;
}

Is CSS, it's entirely in the wrong section.

It goes in between <style> tags within the <head>.

Code:
<style type="text/css">
.difmarg {
   margin-left:5px;
}
</style>



[monkey][snake] <.
 
yeah thought so.. i was gonna say.. do i put the css code in my original style sheet..

i thought you could embed some css within the javascript.. ok let me try it and see if it works...

do you like my site?
 
ive definately ruined the script.. the actual drop down doesnt work now.. here it is.. ive put the css class in my stylesheet..

can you just spot the extra } or the mistake in this please..

Code:
<script type="text/javascript">




  // an array to hold the contents of all lists
  var DataArray = new Array();


***** my array items..




  // swap contents of second list
  function switchList(List1)
  { var i, k;
    var List2 = document.getElementById("SecondList");
    var TypeOfValues = List1.options[List1.selectedIndex].value;

    //Remove all items in SecondList listbox
    //Start removing items at end and work back to front
    k = List2.options.length - 1;
    for (i = k; i >= 0; i--)
    { List2.options[i] = null;
    }

    k = 0;
     for(i = 0; i < DataArray.length; i++)
    { if (DataArray[i][0] == TypeOfValues)
      { List2.options[k] = new Option(DataArray[i][1], DataArray[i][2]);  
        if (DataArray[i][1] == "WC1 - Bloomsbury") {
           List2.options[k].className = "difmarg";
        }   
        k++;
      }
    }
</script>
 
ok. i was missing a } at the bottom of the script.. so basically right now i have this..

Code:
<script type="text/javascript">




  // an array to hold the contents of all lists
  var DataArray = new Array();


***** my array items..




  // swap contents of second list
  function switchList(List1)
  { var i, k;
    var List2 = document.getElementById("SecondList");
    var TypeOfValues = List1.options[List1.selectedIndex].value;

    //Remove all items in SecondList listbox
    //Start removing items at end and work back to front
    k = List2.options.length - 1;
    for (i = k; i >= 0; i--)
    { List2.options[i] = null;
    }

    k = 0;
     for(i = 0; i < DataArray.length; i++)
    { if (DataArray[i][0] == TypeOfValues)
      { List2.options[k] = new Option(DataArray[i][1], DataArray[i][2]);  
        if (DataArray[i][1] == "WC1 - Bloomsbury") {
           List2.options[k].className = "difmarg";
        }   
        k++;
      }
    }
}
</script>

the menu is loading the postcodes correctly but the WC1 postcode doesnt have a margin..

i have this code in my stylesheet (external)
Code:
.difmarg {
   margin-left:5px;
}

why isnt it working?
 
You can't assign a margin to an <option> tag.

The only way to do that is a dirty method by adding &nbsp;'s in front of the value.


[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top