Jump to content

[SOLVED] Simple variable question


rhyspaterson

Recommended Posts

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! :D

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.