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

Page 1 of 1

1 Replies - 9647 Views - Last Post: 28 September 2008 - 07:53 AM

#1 adamgram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-September 08

using javascript to change selection on a drop down menu?

Posted 28 September 2008 - 05:17 AM

This is similar to another problem I was having with text inputs and posted about here http://www.dreaminco...wtopic64855.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:

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

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

Is This A Good Question/Topic? 0
  • +

Replies To: using javascript to change selection on a drop down menu?

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1201
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: using javascript to change selection on a drop down menu?

Posted 28 September 2008 - 07:53 AM

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.

<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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1