Solved

Trying to hide a div with javascript function

Posted on 2013-06-25
9
292 Views
Last Modified: 2013-07-05
Hello
In aspx, I need to hide a <div> with javascript, based on certain condition.

I tried to code:

<SCRIPT language="javascript">
 function ShowHide(xx) {

            if (xx == 'condition') {
                return "display:none;";
            }
            else {
                return "";
            };
        }	
</SCRIPT>

<div  style='javascript:ShowHide("condition");'> 
               Hello
</div>     

Open in new window

But no success. Word Hello is still showing.
0
Comment
Question by:miyahira
9 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39276865
Javascript events must be in something like 'onclick', not in a style attribute.  More info here: http://www.w3schools.com/jsref/dom_obj_event.asp
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39276963
Try this one:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(document).ready(function(e) {
    var myval=$('#mydiv').attr('style');
	if(myval=='condition'){
		$('#mydiv').show();
	}else{
		$('#mydiv').hide();
	}
});
</script>
</head>
<body>
<div id="mydiv" style="condition">Hello</div>     
</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:miyahira
ID: 39278003
No success.
Is there a way to hide or show a div programmatically not with server code but with client code?
0
 
LVL 82

Expert Comment

by:hielo
ID: 39278077
If all you are trying to do is show/hide, there is no need for a "condition" parameter.  You can extract the css display property from the target element (the element you are trying to show/hide) and based on its value, you either show it or hide it.  The code below is client side code.
<script type="text/javascript">
function ShowHide(targetElement, triggerElement) {
	targetElement.style.display = targetElement.style.display=='none'?'':'none';
	triggerElement.value = targetElement.style.display=='none'?'Show':'Hide';
}	
</script>
<input type="button" value="Hide" onclick="ShowHide( document.getElementById('theDiv'), this )" />
<div id="theDiv"> 
               Hello
</div>     

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:Zeickan
ID: 39278995
Try this:

<SCRIPT language="javascript">
 function ShowHide(id) {

 	var box = document.getElementById(id);
 	var xxx = box.style.display;

    if (xxx == 'none') {
       box.style.display = "block"
    }
    else {
       box.style.display = "none";
    };
}	
</SCRIPT>

<div id="example">Hello</div>     

<a href="javascript:ShowHide('example');">Show/Hide</a>

Open in new window

0
 
LVL 18

Expert Comment

by:nap0leon
ID: 39279642
If you just want to always hide it, just put this at the bottom of the page:
<script type="javascript/text">
document.getElementById('your_id_goes_here').style.display = 'none';
</script>

Open in new window


Ideally, this would go into a document-ready or window-load function as described above.

Or... use the ShowHide function previously posted Zeikcan and place a call to that function as described in the code sample above.
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39280086
Did you check what i post on previous comment. It's for concept what is your progress??
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39280203
From another question today http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_28168426.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
// hide div
document.getElementById('theHint').style.visibility = 'hidden';
document.getElementById('theHint').style.display = 'none';
}
// -->
</script>
</head>
<body>


<div id="theHint">
<input type="checkbox" onclick="setCookie('newmember','off',10000)" />If you check this checkbox, this hint will not show again.<br />
This is a hint</div>
</body>
</html>

Open in new window

0
 
LVL 6

Expert Comment

by:lightspeedvt
ID: 39282888
Here is the working code:
<SCRIPT language="javascript">
 function ShowHide(obj, xx){
            if (xx == 'condition') {
                obj.style.display = 'none';
            }
        }	
</SCRIPT>

<div onClick='ShowHide(this, "condition");'>
    Hello
</div>

Open in new window


Demo:
http://jsfiddle.net/BxuCq/
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Download a website to hdd 2 48
Change text to radio button and calendar form 2 32
Message not shown 5 29
replace all occurrences within a string 8 12
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now