LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2013, 06:45 AM   #1
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Rep: Reputation: 15
Javascript to display multiple text box


Hello all,

I am trying to display two text box in javascript when option is selected, but I am not able to succeed. Below is the code:

function checkType(val)
{
if(val=="type1")
document.getElementById('fix_bw').style.display='block';
else
document.getElementById('fix_bw').style.display='none';
if(val==="type2")
document.getElementById('assure_bw').style.display='block';
else
document.getElementById('assure_bw').style.display='none';
if(val=="type3")
document.getElementById('max_bw').style.display='block';
document.getElementById('assure_bw').style.display='block';
else
document.getElementById('max_bw').style.display='none';
document.getElementById('assure_bw').style.display='none';
}


<tr>
<td bgcolor="E1E1FF">Bandwidth Type</td>
<td bgcolor="F4F4FF"><select name="bwtype" type="text" id="bwtype" size="1" onchange='checkType(this.value);' />
<option value="type1" title="Fixed Bandwidth" onmouseover="getText(this);">Type1</option>
<option value="type2" title="Assured Bandwidth" onmouseover="getText(this);">Type2</option>
<option value="type3" title="Assured + Maximum Bandwidth" onmouseover="getText(this);">Type3</option>
<option value="type4" title="Maximum Bandwidth" onmouseover="getText(this);">Type4</option>
<option value="type5" title="Fixed + Assured + Maximum Bandwidth" onmouseover="getText(this);">Type5</option></td>
<td bgcolor="E1E1FF">Fixed Bandwidth(Kbit/s):</td>
<td bgcolor="F4F4FF"><input name="fix_bw" type="text" id="fix_bw" style='display:block;' /></td>
</tr>
<tr>
<td height="25" bgcolor="E1E1FF">Assure Bandwidth(Kbit/s):</td>
<td bgcolor="F4F4FF"><input name="assure_bw" type="text" id="assure_bw" style='display:none;' / ></td>
<td height="25" bgcolor="E1E1FF">Max Bandwidth(Kbit/s):</td>
<td bgcolor="F4F4FF"><input name="max_bw" type="text" id="max_bw" size="18" style='display:none;' /></td>
</tr>
 
Old 04-01-2013, 07:26 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
You forgot about curly braces on "type3" check:
Code:
function checkType(val)
{
if(val=="type1")
  document.getElementById('fix_bw').style.display='block';
else
  document.getElementById('fix_bw').style.display='none';

if(val==="type2")
  document.getElementById('assure_bw').style.display='block';
else
  document.getElementById('assure_bw').style.display='none';

if(val=="type3")
  {
  document.getElementById('max_bw').style.display='block';
  document.getElementById('assure_bw').style.display='block';
  }
else
  {
  document.getElementById('max_bw').style.display='none';
  document.getElementById('assure_bw').style.display='none';
  }
}
Please also use a CODE tags around you code for better readability.
 
Old 04-01-2013, 08:49 AM   #3
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Hi,

I put curly braces in type3, now type2 is not displayed. Even I tried by putting/removing curly braces in type2. Any help?

function checkType(val)
{
//Display fix_bw box on selection of type1
if(val=="type1")
document.getElementById('fix_bw').style.display='block';
else
document.getElementById('fix_bw').style.display='none';
//Display assure_bw text box on selection of type2
if(val==="type2")
{
document.getElementById('assure_bw').style.display='block';
}
else
{
document.getElementById('assure_bw').style.display='none';
}
//Display both max_bw and assure_bw box on selection of type3
if(val=="type3")
{
document.getElementById('max_bw').style.display='block';
document.getElementById('assure_bw').style.display='block';
}
else
{
document.getElementById('max_bw').style.display='none';
document.getElementById('assure_bw').style.display='none';
}
}

Last edited by ohcarol; 04-01-2013 at 08:55 AM.
 
Old 04-01-2013, 10:11 AM   #4
Boring
LQ Newbie
 
Registered: Nov 2005
Posts: 12

Rep: Reputation: 0
Hi,

When type2 is selected, function checkType is called.

it will check val=="type1", if not equal to type1, it will run this:
document.getElementById('fix_bw').style.display='none';

Next, it will check val==="type2", if equal to type2, it will run this:
document.getElementById('assure_bw').style.display='block';

Next, it will check val=="type3", if not equal to type3, it will run this:
document.getElementById('max_bw').style.display='none';
document.getElementById('assure_bw').style.display='none';

Take note there, the 'assure_bw' become 'none';
type2 and type3 are calling the same ElementID 'assure_bw', type2 want it to display, but type3 want it not to display.
The last action win, which is type3 document.getElementById('assure_bw').style.display='none';
Therefore, type2 is not displayed.

Last edited by Boring; 04-01-2013 at 10:12 AM. Reason: typo
 
Old 04-01-2013, 10:16 AM   #5
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Hi Boring,

So what will be the best option to display two input text box at once upon option selection? Please help me as I don't have much language of programming.


thank you
 
Old 04-01-2013, 10:19 AM   #6
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Well, your logic code is broken. When you select "type2" option then "else" block from "type3" is also executed, which hide "assure_bw". Use this code:
Code:
function Show(aId, aState)
  {
  document.getElementById(aId).style.display = aState ? 'block' : 'none';
  }


  
function checkType(val)
  {
  Show('fix_bw', (val == "type1"));
  Show('assure_bw', (val == "type2") || (val == "type3"));
  Show('max_bw', (val == "type3"));
  }
That way you can easily add another conditions, when and what should be displayed.
 
Old 04-01-2013, 10:52 AM   #7
ohcarol
Member
 
Registered: Dec 2004
Location: Nepal
Posts: 86

Original Poster
Rep: Reputation: 15
Thank you eSelix, It solved my problem.


thank you very much
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] JavaScript - input box czezz Programming 4 07-26-2011 06:33 PM
[SOLVED] javascript fancy a box? trscookie Programming 3 02-23-2010 09:07 PM
Grabbing text from a text box in a window running in wine. thefalling881 Programming 6 07-21-2008 08:38 PM
Combine multiple one column text file into one text file with multiple colum khairilthegreat Linux - Newbie 7 11-23-2007 01:31 PM
Bug A Javascript that will not allowed alphabet character type into a text box Linh Programming 2 09-22-2003 11:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration