Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 3rd, 2005, 03:04 PM
Authorized User
 
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default show/hide text field from select (drop down)

Hi,
I browsed through 25 pages of threads on this forum plus google last night to find the solution, but I wasn't able to.
I am new to Javascript, as a matter of fact, never wrote a single javascript line:

What I am trying to do is add a textfield right after a drop down menu only if the user selects a specific option, that being the custom field.

Here is my select statement:
-------------------------------------------

<select class="text" name="companyname">


      <?php
do {
?>
      <option value="<?php echo $row_1['name']?>"<?php if (!(strcmp($row_1['name'], $row_1['name']))) {echo "SELECTED";} ?>><?php echo $row_1['name']?></option>
      <?php
} while ($row_1= mysql_fetch_assoc($company1));
  $rows = mysql_num_rows($company1);
  if($rows > 0) {
      mysql_data_seek($company1, 0);
      $row_1= mysql_fetch_assoc($company1);
  }
?>
      <option value="NULL" Selected>Select Company name</option>
      <option value="Not in this List">Custom</option>

</select>




-----------------------------------------------


Any help would be appreciated.

__________________
----------------
Never bother to learn something not knowing which does not do you any harm, and never neglect to learn something whose negligence will increase your ignorance - Imam Jafar Sadeq
 
Old January 4th, 2005, 12:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

There are two things you need to work out here.

First you need determine when the dropdown list has the Custom value selected/OR NOT.
Second you need to show/HIDE the textbox.

Code:
<script language="JavaScript">
function testCustom(selObj)
{
if(selObj.options[selObj.selectedIndex].value == 'Not in this List')
{
//Show the textbox
document.yourtextboxname.style.visibility = 'visible';
document.yourtextboxname.style.display = '';
}
else
{
//Hide the textbox
document.yourtextboxname.style.visibility = 'hidden';
document.yourtextboxname.style.display = 'none';

}
}
</script>
The select control needs to have a onchange event
Code:
onChange="javascript:testCustom(this);"
I have not tested the syntax which is important as javascript is case sensitive.
But this should get you started.
To find out more search for these issues seperately.
IE Show Hide textbox in javascript
AND Get Selected Value in Javascript







======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old January 4th, 2005, 11:56 AM
Authorized User
 
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for replying.
Unfortunately it hasn't worked yet. I put the script in the <head> section, created a textfield named it custom, and then changed the "yourtextboxname" to "custom." in the script. Also added the onchange event.

still nothing happenining.
Do you think it has to do with the fact that the menu is pulling dynamic data?



 
Old January 4th, 2005, 08:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

No, it is not related to the data being dynamic.
Did you put the client side event in the <Select> tag?
Code:
<select class="text" name="companyname" onChange="javascript:testCustom(this);"
>
Put an alert command in the javascript function to see that it is at least getting that far.
Code:
alert('Made IT');

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old January 4th, 2005, 08:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Here is another example of the show hide part.
http://www.codepedia.com/1/How_To_Ja...n_Button_Click
You should break this problem down and see which parts are working and which are not.
1, Is the onchange event occuring?
2, Is the test to match the 'Not in this List' text working as it should?
3, Is the show hide aspect working?

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old January 5th, 2005, 03:33 PM
Authorized User
 
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.
I will work on it today to see how it goes.


 
Old December 7th, 2005, 05:44 AM
Registered User
 
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i couldn't get this to work either.
there is an example that works here
http://www.sitepoint.com/forums/prin...d.php?t=319080





Similar Threads
Thread Thread Starter Forum Replies Last Post
populating drop down through text field nasirmunir Javascript How-To 5 June 30th, 2008 11:01 AM
HIDE/SHOW pallone Javascript How-To 11 April 16th, 2007 10:24 AM
Pass select values to hidden text field? abq23 Javascript How-To 2 July 20th, 2004 09:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.