Jump to content

Hide & Show


codingmasterRS

Recommended Posts

Hi Guys,

Basically I am after some JS or Jquery code that will allow me to do the following:

 

I have a textarea which when click needs to trigger a div tag to be shown, then when a user clicks out of this textarea the div should disappear again.

 

I have this code:

        <script>
		function dispChange(eid){
			if(document.getElementById(eid).style.display=="none")
			{
				document.getElementById(eid).style.display="none";
			}
			else
			{
				document.getElementById(eid).style.display="block";
			}
		}
	</script>

 

<body onClick="dispChange('hideme')">
<textarea onclick="dispChange('hideme')></textarea>
<div id="hideme">HIDEME</div>

 

Now it works (sort of), but when it is display:none; and the  the user click in the body, the div appears, how do I stop this? Also it does NOT hide when I click into the body.

 

Cheers in advance.

Link to comment
Share on other sites

from what you posted, i believe you might want something like this.

$(document).ready(function(){
$("#textarea").focus(function(){
	$("#hideme").show("normal");
});
});

$(document).ready(function(){
$("#textarea").blur(function(){
	$("#hideme").hide("normal");
});
});

now you will have to assign an id to your textarea and replace my #textarea with whatever your id is preceded by a #. this code will make the div appear when you click inside of the textarea, and disappear when you click out of it, hope this helps.

 

Also, the "normal" parameter in both .blur() and .focus() can be interchanged with "slow" or "fast", depending on what you want the speed to be of the div hiding or showing

 

Edit: see that you found a solution, please mark as solved

Link to comment
Share on other sites

Just a word of caution, by setting you jquery to display the div while any textarea is clicked, you might get unwanted results. If it is only that specific textarea that you wish to have the div display onfocus, I would advise assigning a unique id to you textarea tag and call it like I have shown in my code. Unless you want all textareas to display the div of course

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.