Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

javascript question

  • 23-07-2007 12:10PM
    #1
    Closed Accounts Posts: 2,349 ✭✭✭


    I have a bunch an unknown amount of <SELECT> things that will be spewed out by a PHP script like so:
    <select id="control_$count">
    <option value="$id1">$name1
    <option value="$id2">$name2
    <option value="$id3">$name3
    <option value="$id4">$name4
    <option value="$id5">$name5
    </select>
    

    Where $count goes 0,1,2,3 each time a new <SELECT> is printed and $id and $name come from database values.

    I need to set the default item that will be selected when the page loads.
    To do this I need to iterate through each value and compare it with a value from the database.

    So I try:
    var control = document.getElementById("control_$count")
    
    for (var x=0; x<control.length; x++) {
      if (control.value[x] == $variablefromdb)
        control.selectedIndex = x;
    }
    

    However it appears the control.value thing is not an array. If I refer to value[0] it returns the value of the selected option, but value[1]... is undefined.


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    To access the array of values, you need the .options[] property.

    So try
    var control = document.getElementById("control_$count")
    
    for (var x=0; x<control.length; x++) {
      if (control.options[x].value == $variablefromdb)
        control.selectedIndex = x;
    }
    


  • Moderators, Society & Culture Moderators Posts: 9,688 Mod ✭✭✭✭stevenmu


    Could you do the comparison in your php code ? The php code should output something like
    <select id="control_$count">
    <option value="$id1">$name1
    <option value="$id2">$name2
    <option selected value="$id3">$name3
    <option value="$id4">$name4
    <option value="$id5">$name5
    </select>
    

    where $id3 is obviously the one that matches your database field. This way you can also compare against the value submitted from the form if the page needs to reload, allowing any choices made by the user to persist across page reloads.


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    OK, thanks. curses. I can do it that way but I'll have to rewrite the code a bit more, it's already becoming spaghetti.

    I'm writing a highly configurable CMS so I need to put an unknown amount of controls in unknown places, replacing values and stuff is proving to be difficult due to the differences in implementing different types of form control.

    Argh.

    Thanks for your replies, I'll have a go.


  • Closed Accounts Posts: 45 CelloPoint


    grasshopa wrote:
    I need to set the default item that will be selected when the page loads.

    Try something like:
    <?php
    		$lines=file('CarMakes.txt');
    		foreach($lines as $line_num=>$line){
    			//php to set default selection (SELECTED)
    			if($line=="Toyota\n"){
    				$default=" SELECTED";
    			}
    			else{$default="";}
        			echo "<option value='".htmlspecialchars(str_replace(" ","",$line))."'".$default.">".htmlspecialchars($line)."</option>\n";
    		}
    ?>
    

    where CarMakes.txt would contain a list of car makes: Toyota\nFord\nAlfa\nBentley... etc.


  • Closed Accounts Posts: 2,349 ✭✭✭nobodythere


    Thanks for your reply, but I coded my solution in JS and don't wanna wreck my head :)
                var drop = document.getElementById('control_$cycle');
                if (drop.hasChildNodes())
                 {
                   var children = drop.childNodes;
                   for (i = 0; i < drop.length; i++) 
                   {
                        console.log(children" . $cycle . "[i]);
    .... Extra code
                   };
                 };
    


    Funny little quiff here though is that a newline character shows up as a child so I had to take them all out


  • Advertisement
Advertisement