Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get value from dropdown issue... 2

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
Hi,

Have this working on other projects but havent used in a while - my problem is 's' is displayed as [object] instead of dropdown 'x_typeid' value and I dont know why

Code:
function moveidwarr(){
    var s = document.fzresponseadd.x_typeid;
 document.fzresponseadd.x_emailtext.value = s;	
}
[code]

x_typeid is got from this dropdown...

[code]
<select id='select' name='x_typeid' title='' onchange = 'moveidwarr()'>
<option value="" selected>
Please Select</option>
<option value="2">
Email</option>
<option value="3">
Fax</option>
<option value="1">
Phone</option>
</select>

form head looks like
Code:
<form name="fzresponseadd" id="fzresponseadd" action="zresponseadd.asp" method="post" onSubmit="return ew_ValidateForm(this);">
 
That's to be expected as you are assigning the object itself to the value of the 'x_emailtext' field, rather than the value of the select element.

Try this instead:

Code:
function moveidwarr() {
	var frmObj = document.forms['fzresponseadd'].elements;
	var selObj = frmObj['x_typeid'];
	if (selObj.selectedIndex == -1) {
		frmObj['x_emailtext'].value = 'No option has been selected';    
	} else {
		frmObj['x_emailtext'].value = selObj.options[selObj.selectedIndex].value;    
	}
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Simply this.
>var s = document.fzresponseadd.x_typeid;
[tt]var s = document.fzresponseadd.x_typeid[red].value[/red];[/tt]
 
hi, am taking this a step further and need another selectbox to show if the value of x_typeid = 1

have started by trying to show a hidden div

have put a hidden div on the page with the second combo in it

Code:
<div id="div1" style="visibility:hidden">
<select id='x_emailmessage' name='x_emailmessage' title=''>
<option value="" selected>
Please Select</option>
<option value="1">
Vehicle enquiry</option>
</select>
</div>
[code]

got this function that shows a div
[code]
function showMe (it, box) {
  //var vis = (box.checked) ? "visible" : "hidden";
  document.getElementById(it).style.visibility = vis;
  document.layer01.style.visibility = "visible";
}

and added this line to onchange function posted above
Code:
function moveidwarr(){
var frmObj = document.forms['fzresponseadd'].elements;    var selObj = frmObj['x_typeid'];
	
if (selObj.selectedIndex == 1) {
frmObj['x_emailtext'].value = 'Email will be sent when ADD is clicked !!';
frmObj['x_notes'].value = 'Hi - thankyou for your enquiry\n\n' + (toemailObj.value);
[COLOR=#ff0000]showMe('div1', this);[/color]
document.fzresponseadd.x_notes.focus();
} else {
frmObj['x_emailtext'].value = ''
}
}

any ideas welcome
 
Forget about the function you've found from unknown source, it may not be the convenient one for this situation. You can simply devise one yourself! I would say this.
[tt]
function moveidwarr(){
var frmObj = document.forms['fzresponseadd'].elements;
var selObj = frmObj['x_typeid'];

if (selObj.selectedIndex == 1) {
frmObj['x_emailtext'].value = 'Email will be sent when ADD is clicked !!';
frmObj['x_notes'].value = 'Hi - thankyou for your enquiry\n\n' + (toemailObj.value); [red]//what is toemailObj, I haven't got a clue[/red]
//showMe('div1', this);
[blue]document.getElementById("div1").style.visibility="visible";[/blue]
frmObj["x_notes"].focus(); //[blue]better use consistent convention, one way or another[/blue]
} else {
frmObj['x_emailtext'].value = '';
[blue]document.getElementById("div1").style.visibility="hidden";[/blue]
}
}[/tt]

Mind the layout impact of visibility vs display. Also the admissible set of values of it against different browser's versions - just a side-note to check.
 
thanks tsuji - heres a *
[toemailobj was a test - i removed the declarations but forgot about that bit!]
 
hi, am using a similar script in a new project and the divs show/hide on the wrong drop down options.
If i choose 'Phone' it runs the [4] Case which is the option for text.
Can anyone help me see why?

decided to use CASE instead of IF on this function
Code:
function moveidwarrCASE(){
    var frmObj = document.forms['fzresponseadd'].elements;
    var selObj = frmObj['x_typeid'];

switch (selObj.selectedIndex) {
//Email show
case 2: 
	hide('text');
	show('email');
javascript:alert('Email will be sent when ADD is clicked');
	break
//Text show
case 4: 
    hide('email')
    show('text'); 
inserttemplate();
javascript:alert('Text will be sent when ADD is clicked');
	break
//everything else
default: 
	hide('text');
	show('email');
	break
}
}

html
Code:
<form name="fzresponseadd" id="fzresponseadd" action="zresponseadd.asp" method="post">
<select id='x_typeid' name='x_typeid' title='' onchange='moveidwarrCASE()'>
<option value="" selected>Please Select</option>
<option value="5">Cold Call</option>
<option value="2">Email</option>
<option value="3">Fax</option>
<option value="1">Phone</option>
<option value="4">Text</option>
</select>

<div id="email" style="display: none;"> 
<textarea name="x_notes" id="x_notes" title="" cols="35" rows="4"></textarea>
</div>

<div id="text" style="display: none;"> 
<textarea name="x_notes2" id="x_notes2" title="" cols="30" rows="6" onKeyPress="return taLimit(this)" onKeyUp="return taCount(this,'myCounter')" wrap="physical"></textarea>
</div>
</form>
 
hi - thanks for your reply - tried

Code:
var valObj = document.getElementById("x_typeid").selectedvalue;
	
switch (valObj) {
etc...

but still doesnt work
 
Hi

Do not try to guess the syntax. Leads nowhere.
Code:
var valObj = document.getElementById("x_typeid")
var valVal = valObj.options[valObj.selectedIndex].value

switch (valVal) {
[gray]etc...[/gray]

Feherke.
 
Which shouldn't need guessing anyway, as I gave this syntax in my first post to this thread!

Sometimes I wonder if people actually learn from code given, or whether they just copy it and hope it works.

Dan





Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
ok thanks

Code:
function moveidwarrCASE(theSel){
var valObj = (theSel[theSel.selectedIndex].value);
switch (valObj) {
//Email show
case '2': 
etc...
note - had to put my values in single quotes aswell

Code:
<select id='x_typeid' name='x_typeid' title='' onchange='moveidwarrCASE(this)'>
etc...
</select>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top