rhyspaterson Posted July 10, 2007 Share Posted July 10, 2007 Hey lads, Writing a whole load of PHP code and realized i needed to use a tiny bit of JavaScript (which i know nothing about). I have a line that looks like this: document.clientCategoriesForm.adultContent.options.length=0; This is inside a FOR loop which uses 'i' as the loop variable, and i wanted to replace .adultContent. with .i. above, but now sure how to do it. In PHP it would be; document.clientCategoriesForm.$i.options.length=0; What is the JS equivalent? Cheers! Quote Link to comment Share on other sites More sharing options...
arianhojat Posted July 10, 2007 Share Posted July 10, 2007 Not sure what you want.... Set everything inside of clientCategoriesForm to 0? document.clientCategoriesForm.options.length=0; or mess with options themselves document.clientCategoriesForm.adultContent.options Quote Link to comment Share on other sites More sharing options...
xenophobia Posted July 10, 2007 Share Posted July 10, 2007 Just replace with 'i' without the dollar sign: document.clientCategoriesForm.$i.options.length=0; Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 11, 2007 Author Share Posted July 11, 2007 Hey guys, Figured i would give you a bit more info to help me figure this out, heh. Basically i have 32 select menus, each with two states, allow and block. Using PHP i am connecting to a PostgreSQL database and defining the initial states of these select menus when the pages loads (based on the data in the database). This is all fine. What i would like to do now, and can't do with PHP (at least i think i can't), is have a button that will change all the select menus to allow or block. Based on my research i came up with this code: <select name="0"> // names will go from 0 to 32 <?php if ($line[0][1] == 0) { echo '<option value="1">Block</option>'; echo '<option value="0" selected="selected">Allow</option>'; } else { echo '<option value="1" selected="selected">Block</option>'; echo '<option value="0">Allow</option>'; } ?> </select> function changeStateAllow() { for (i=0; i<32; i++){ document.clientCategoriesForm.i.options.length=0; document.clientCategoriesForm.i.options[0]=new Option("Allow", "0", true, false); document.clientCategoriesForm.i.options[1]=new Option("Block", "1", false, false); } } </script> ... <input type="button" value="Allow All" onclick="changeStateAllow()" > What i wanted to happen is, with my select menus named 0,1, 2, 3, 4 .. all the way to 32, run this loop when a button is clicked, and change each state to allow. Now i know i probably don't need to delete all the options and recreate them, but i just wanted to get the damn thing to work first before improving the coding, haha. Unfortunately this code doesn't work, and i am a bit confused as to how to go about it now. Could anyone point me in the right direction? Thanks heaps guys Quote Link to comment Share on other sites More sharing options...
mainewoods Posted July 11, 2007 Share Posted July 11, 2007 like this: for (i=0; i<32; i++){ document.clientCategoriesForm[i].options.length=0; Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 11, 2007 Author Share Posted July 11, 2007 Thanks for the reply! Did some testing (as it did not work), and think i may have figured out why. When i run the code: document.clientCategoriesForm.x.selectedIndex = 1; (where x is the name of the select menu), when x is a number it does not work, but if it is a word, the function does. Any reason for that? (obviously i am changing the select names to words or numbers for the testing heh) I.e This works: <script type="text/javascript"> function changeStateAllow(){ document.clientCategoriesForm.adultContent.selectedIndex = 1; document.clientCategoriesForm.wwwEmailSites.selectedIndex = 1; document.clientCategoriesForm.news.selectedIndex = 1; document.clientCategoriesForm.violenceUndesirable.selectedIndex = 1; } </script> This does not: <script type="text/javascript"> function changeStateAllow(){ document.clientCategoriesForm.1.selectedIndex = 1; document.clientCategoriesForm.17.selectedIndex = 1; document.clientCategoriesForm.2.selectedIndex = 1; document.clientCategoriesForm.18.selectedIndex = 1; } </script> Cheers Quote Link to comment Share on other sites More sharing options...
mainewoods Posted July 11, 2007 Share Posted July 11, 2007 the way to use variables in dom access is the way I showed using the brackets. But I believe the .length property in select items is read only: http://www.w3schools.com/htmldom/dom_obj_select.asp Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 11, 2007 Author Share Posted July 11, 2007 Ah sorry, i figured it out haha. I had a . in the wrong place. document.clientCategoriesForm.[i].selectedIndex = 1; should have been document.clientCategoriesForm[i].selectedIndex = 1; Thanks so much for your help Quote Link to comment Share on other sites More sharing options...
mainewoods Posted July 11, 2007 Share Posted July 11, 2007 the rule with the []s is like this: -do not use . before the brackets -between the brackets you can put a number, a string, or a js var document.formname['literalfieldname'].propertyname = ' '; document.formname[2].propertyname = ' '; document.formname[jsvariable].propertyname = ' '; -if I wanted to use js vars for the formname as well as the field name: document[myformname][myfieldname].propertyname = ' '; Use the []'s to use js vars to access the dom, don't use eval() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.