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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do this? 3

Status
Not open for further replies.

youradds

Programmer
Joined
Jun 27, 2001
Messages
817
Location
GB
Hi,

Hopefully a quickie for you gurus :D

Heres what I'm trying to do -

1) Have a SELECT field, which has different values (in my case, as "Description") .. i.e;

Code:
<SCRIPT language="JavaScript">
<!--
function stat_write(thetext) {
//   alert("ergrdfg");
   this.document.foobar.value = thetext;
} 
//-->
</SCRIPT>

	<select size="1" name="D1">
	  <option onselect="stat_write('your words 3')" >dfgdfgdg</option>
  	  <option onselect="stat_write('your words 3')" >dfgdfgdfg</option>
	  <option  onselect="stat_write('your words 3')" >dgdfgdfg</option>
	</select>

(removed the <head><html> etc parts, to make it shorter and not waste so much space in the post :))

...then a form field, called:

Code:
    <input type="text" readonly="yes" value="cat desc" name="foobar" >

Basically when someone click one an option from the above "SELECT", I'm trying to get it to then show the content of that field in the "foobar" input box bit.

Am I going about this the wrong way? :/

NB: I need to keep the "value" for the actual select field, i.e;

Code:
	  <option value="[B]NUMERIC_ID[/B]" onselect="stat_write('your words 3')" >dfgdfgdg</option>

TIA for ANY help you can provide. I've hit a real head stomper on this one :(

Cheers

Andy
 
You could try
document.getElementByName("foobar").value = thetext

Otherwise, it looks right to me.
 
Hi,

Thanks for the reply. That still gives me an error :/

Error: document.getElementByName is not a function
Source File: file:///C:/Documents%20and%20Settings/Owner/My%20Documents/My%20Webs/stertert.htm#
Line: 5

Script is now as follows:

Code:
<SCRIPT language="JavaScript">
<!--
function stat_write(thetext) {
     document.getElementByName("foobar").value = thetext
} 
//-->
</SCRIPT>

..and invoked with:

Code:
<a href="#" onclick="stat_write('your words')">stertert</a>

Any more ideas?

Thanks again for your input!

Cheers

Andy
 
It should be
Code:
document.getElementById('foobar').value = 'your text';

This requires that the foobar field have an ID tag with the value foobar.
getElementsByName (plural) would return a collection of objects of the specified name. You would use it when you have multiple fields with the same name and they would be retrieved as an array of objects.




At my age I still learn something new every day, but I forget two others.
 
youradds,

The event handler that you need to use is the "onchange" handler. It is defined by the <select> tag, not the <option> tag (I'm not even sure that "onselect" is a valid handler....)

You will have to write a function to check and see which option has been checked when the select box is changed, and then use that to determine what code should be run.

-kaht

Looking for a puppy?

[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
 
Hi,

Thanks guys - that worked a charm :) (vote coming both your way's :))

I ended up using:

Code:
<SCRIPT language="JavaScript">
function stat_write(thecatid) {

   var catdescs = new Array()
   catdescs[0] = "Saab"
   catdescs[1] = "Volvo"
   catdescs[2] = "BMW"  

    document.getElementById('foobar').value = catdescs[thecatid] ;
} 	
</SCRIPT>


</head>

<body>

<form method="POST">
	<p><select size="1" name="D1" onchange="stat_write(this.value)">
	<option value="1">dfgdfgdg</option>
	<option value="2">dfgdfgdfg</option>
	<option value="3">dgdfgdfg</option>
	</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>

    <input type="text" readonly="yes" value="cat desc" name="foobar" id="foobar" >

</form>

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top