Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,331 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,112 people online right now. Registration is fast and FREE... Join Now!




using javascript to change selection on a drop down menu?

 
Reply to this topicStart new topic

using javascript to change selection on a drop down menu?, I want a menu to change based on a variable whenever a script is run

adamgram
post 28 Sep, 2008 - 04:17 AM
Post #1


New D.I.C Head

*
Joined: 17 Sep, 2008
Posts: 7


My Contributions


This is similar to another problem I was having with text inputs and posted about here http://www.dreamincode.net/forums/showtopic64855.htm

I have an 'edit your post' page where the dropdown menus are selected initially from php variables defined in a MYSQL database. I also have an 'edit' button that enables the user to change what they want to select for each category. I'm now trying to add a 'reset' button that returns things to the way they were before the edit began. I got it to work with the text inputs with some help of a guy on this forum like this:

CODE

<script type="text/javascript">
function rest_name_enable(){
  if(document.all || document.getElementById){
    document.form_name.rest_name.disabled = false;
    document.form_name.rest_name.defVal = document.form_name.rest_name.value;
  }
}

function rest_name_reset(){
  if(document.all || document.getElementById){
    if(document.form_name.rest_name.disabled == false){
      var r = confirm("This will delete whatever you just typed!!\nAre you sure you want to reset the Restaurant Name?");
      if(r == true){
        document.form_name.rest_name.disabled = true;
        document.form_name.rest_name.value = document.form_name.rest_name.defVal;
      }
      else{
      }
    }
    else{
    }
  }
}

</script>

<form name="form_name">
<input name="rest_name" type="text" value="<?php echo $row[rest_name]?>" disabled="disabled">
<a href = "java script:rest_name_enable()">edit</a>
<a href = "java script:rest_name_reset()">reset</a><br>


The initial values for the drop down menus are selected like this:

CODE

<select name='price' disabled="disabled">  
<option <?php if ($row[price]=='1') {echo "selected";} ?> value='1'>Under $10</option>
<option <?php if ($row[price]=='2') {echo "selected";} ?> value='2'>$10-$20</option>
<option <?php if ($row[price]=='3') {echo "selected";} ?> value='3'>Over $20</option>
</select>


And the same code that works to enable/disable the text inputs work for the dropdown menus as well, however, I can't figure out how to get the values to reset. Basically, just as the text input reset script assigns the value of the text field using
CODE

document.form_name.rest_name.value = document.form_name.rest_name.defVal;

I need to find a way of setting the selection in a drop down menu. Thanks in advance!
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 28 Sep, 2008 - 06:53 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,983



Thanked 78 times

Dream Kudos: 1175
My Contributions


Okay, here is a basic script, you will need to change around the event that is called to reset the values, but other than that it is working nicely.

CODE
<script>
  function initSelections(){
    alert("EST");
    sels = document.getElementsByTagName("select");
    for(i=0; i<sels.length; i++){
      createNewSel(sels[i]);
    }
  }
  function createNewSel(obj){
    obj.defSel = obj.selectedIndex;
    obj.oncontextmenu = function(){
      this.selectedIndex = this.defSel;
    }
  }
</script>

<select name='price'>  
  <option value='1'>Under $10</option>
  <option selected="selected" value='2'>$10-$20</option>
  <option value='3'>Over $20</option>
</select>

<script>
  initSelections();
</script>


At the moment when you right click on the ddm it will reset to its default value.

NOTE - there is no oncontextmenu event in some browsers.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 02:53AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month