0

I want to hide the text field when I select from item2 from the list. Here is the relevant part of my code:

<div class="control-group"> <label class="control-label">Select Parent</label> <div class="controls"> <select name="parent_id" class="span6 m-wrap" data-placeholder="Choose a Parent" tabindex="1"> <option value="0" >Select Parent</option> <?php $admin_sql=mysql_query("select * from admin_detail where parent_id='0' and role='2'"); while($Fetch_Admin=mysql_fetch_array($admin_sql)) { ?> <option value="<?php echo $Fetch_Admin['id']; ?>" onClick = "myFunction()" > <?php echo $Fetch_Admin['name'] ;?> </option> <?php } ?> </select> </div> </div> <div class="control-group"> <label class="control-label">Name</label> <div class="controls"> <input type="text" name="name" class="span6 m-wrap" placeholder="Name" required/> </div> </div> <div class="control-group"> <label class="control-label">Login ID</label> <div class="controls"> <input type="text" name="login" class="span6 m-wrap" placeholder="Login ID" required/> </div> </div> <div class="control-group"> <label class="control-label">Password</label> <div class="controls"> <input type="password" name="password" class="span6 m-wrap" placeholder="Password" required/> </div> </div> <div class="control-group" id="op1"> <label class="control-label" >Add Options</label> <div class="controls"> <input type="text" name="option1" class="span6 m-wrap" placeholder="Option1" required/> <input type="text" name="option2" class="span6 m-wrap" placeholder="Option2" required/> <input type="text" name="option3" class="span6 m-wrap" placeholder="Option3" required/> <input type="text" name="option4" class="span6 m-wrap" placeholder="Option4" required/> <input type="text" name="option5" class="span6 m-wrap" placeholder="Option5" required/> </div> </div> 

The associated javascript function is:

<script> function myFunction() { document.getElementById("op1").style.visibility="hidden"; } </script> 

I have also tried the same thing by using a button. When I apply the onClick function to the button it work properly. Can some one show me where I am going wrong or which function should I use for the list.

0

6 Answers 6

3

You've placed your event handler in an onclick attribute of an <option> element. This probably isn't going to work as intended. Instead, add an onchange event handler to its parent <select> element as follows:

<select name="parent_id" ... onchange="document.getElementById('op1').style.visibility=(this.selectedIndex==2)?'hidden':'visible';"> 
Sign up to request clarification or add additional context in comments.

3 Comments

onclick should work when you will click on that particlular option in which it is declared .i cant see any reason for it to not working.
Suppose you navigate to the <select> element by pressing the tab key and use the up/down/enter keys to change its value. Would the onclick handler do anything then?
that is an exception case but as a programmer we should be prepared for the worst case.so +1 for this point.
1
 document.getElementById("op1").style.display="none"; 

Comments

1

It should be:

document.getElementById("op1").style.display="none";

instead of:

document.getElementById("op1").style.visibility="hidden";

1 Comment

Visibility hidden is a valid CSS attribute. It just makes the thing invisible but keeps its place in the layout.
1

For a list like the one you have it is required to add the event handling on the select element.

 <select name="parent_id" id="parent_id" class="span6 m-wrap" data-placeholder="Choose a Parent" tabindex="1" onchange="myFunction()"> <option value="0" >Select Parent</option> <option value="item2" > item2 </option> </select> 

Then your js code could be like,

function myFunction() { if(document.getElementById("parent_id").value=="item2"){ document.getElementById("op1").style.visibility="hidden"; }else{ document.getElementById("op1").style.visibility="visible"; } } 

Example, http://jsfiddle.net/5E8AH/

Also, the difference between display:block and visibility:hidden is that the latter will hide the content but still take the space as if it were visible.

Comments

0

Use this one:

document.getElementById("id").style.display = "none"; 

or

document.getElementById("id").style.visibility = "hidden"; 

Comments

0

try this,

document.getElementById("op1").style.display="none"; 

1 Comment

javascript is case sensitive .please use proper camelcase

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.