Jump to content

[SOLVED] Change All Values


Daney11

Recommended Posts

Hi Guys,

 

Say i have a select box at the top of my page

 

<select class="shoutbox"> 
<option selected>---</option> 
<option value=1>1</option>
<option value=2>2</option>
</select>

 

Then i have outputted all users from my database in a php script

 

<select name="access" class="shoutbox">
<option value=1 selected>1</option>
<option value=2>2</option>
</select>

 

How could i create a javascript peice of code, so that when i select a value in the top form... It changes all the forms that i have outputted from the users database to the value of what i choose?

 

Thanks guys.

Link to comment
Share on other sites

Since you have all the select boxes with a uniform class, you can access them that way. If you put this at the top of your page, you shouldn't have to change your markup at all, either:

window.onload = function() {
  eles = document.getElementsByTagName('select');
  for (i = 0; i < eles.length; i++) {
    if (eles[i].className == 'shoutbox') {
      eles[i].onchange = funciton() {
        x = this.selectedIndex;
        ele = document.getElementsByTagName('select');
        for (i = 0; i < ele.length; i++) {
          if (ele[i].className == 'shoutbox') ele[i].selectedIndex = x;
        }
      };
    }
  }
}

 

I haven't fully tested it, but I believe that will fix it for you as long as all your selects have the "shoutbox" class applied to them.

 

Good luck!

Link to comment
Share on other sites

Thanks for the reply mate. Ive put that code in and it doesnt change the boxes at all mate.

 

Ah, I had some of my javascript referencing confused (That's what I get when I switch between languages). Try this instead for your javascript code. I've tested this one, and it seems to work well:

window.onload = function() {
  eles = document.getElementsByTagName('select');
  for (i = 0; i < eles.length; i++) {
    if (eles[i].className == 'shoutbox') {
		obj = eles[i];
		obj.onchange = setMyIndex;
    }
  }
};

function setMyIndex() {
x = this.selectedIndex;
ele = document.getElementsByTagName('select');
for (i = 0; i < ele.length; i++) {
	if (ele[i].className == 'shoutbox') ele[i].selectedIndex = x;
}
}

Link to comment
Share on other sites

That code works mate, however

 

<select class="shoutbox"> 
<option selected>---</option> 
<option value=1>1</option>
<option value=2>2</option>
</select>

 

<select name="access" class="shoutbox">
<option value=1 selected>1</option>
<option value=2>2</option>
</select>

 

As you can see at the top i have a value of "---" "1" and "2" which is the one that can change them all.  But when i drop down and select value 1, it changes all the values below to value 2, which is wrong and confusing hehe. Any ideas mate?

 

Thanks a lot again

 

Dane

 

Link to comment
Share on other sites

Are you only wanting your first dropdown to be the one that changes the rest? I understood it that you wanted all the dropdowns to change all the other ones. If you want to do it a little more "loosely" by selecting the values that have been changed, you can do this (replace the second javascript function with this:

function setMyIndex() {
  myVal = this.options[this.selectedIndex].value;
  ele = document.getElementsByTagName('select');
  for (i = 0; i < ele.length; i++) {
    if (ele[i].className == 'shoutbox') {
      obj = ele[i];
      for (x = 0; x < obj.options.length; x++) {
        if (obj.options[x].value == myVal) obj.selectedIndex = x;
      }
    }
  }
}

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.