admin管理员组文章数量:1316980
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).
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).
Share Improve this question asked Sep 8, 2011 at 14:29 AkosAkos 2,0076 gold badges28 silver badges42 bronze badges 2- 1 Is the span already present or are you creating it on the fly? Also, what do you have so far at all? – pimvdb Commented Sep 8, 2011 at 14:34
- The span is already present. It is hidden with: style="display: hidden;" – Akos Commented Sep 8, 2011 at 14:36
2 Answers
Reset to default 5Most 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>
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./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./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>
本文标签: javascriptonClick on textfield to show a divStack Overflow
版权声明:本文标题:javascript - onClick on textfield to show a div? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742016270a2413830.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论