Reputation: 2007
I have a textarea:
<textarea cols="10" rows="5">Type in the text</textarea>
I want to show a div (or a <span>
) below the textarea when onclick on the textarea.
How could I do this?
Also I would like to hide it when a link is clicked in the div (or span).
Upvotes: 0
Views: 6252
Reputation: 982
Most basic way is to give an id to your span, and then:
<textarea cols="10" rows="5" onclick="document.getElementById('box').style.display='inline';">Type your text here</textarea>
<span id="box" style="display:none">display</span>
Upvotes: 5
Reputation: 14051
If you use jQuery then it's simple:
$("textarea").bind("focus", function(){
$("span").show();
});
Then for the link give it an ID in the HTML:
<span>
<a href="#" id="closeme">Close me</a>
</span>
And then:
$("#closeme").bind("click", function(){
$("span").hide();
});
Remember the Javascript must go inside <script></script>
tags and also ensure you include jQuery in your page using:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
If you are new to jQuery then this code below should give you an idea of how to get started. Generally, it's better to refer to elements using IDs instead of their tag name such as textarea
and span
- It will mean that the javascript will target the right elements.. Something like this will do what you are asking about:
<html lang="en">
<body>
<textarea id="form-details"></textarea>
<span id="form-details-info">
Some info about the textarea
<br/>
<a href="#">Close text area</a>
</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(function(){
// When a user is using the textarea
$("#form-details").bind("focus", function(e){
// Show the span info
$("#form-details-info").show();
});
// When a user clicks the close link
$("#form-details-info a").bind("click", function(e){e){
// Hide the info
$("#form-details-info").hide();
// And use this to stop a prevent a link from doing what it normally does..
e.preventDefault();
});
});
</script>
</body>
</html>
Upvotes: 3