最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onClick on textfield to show a div? - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 5

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>

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>
发布评论

评论列表(0)

  1. 暂无评论