-1

Let's say I have a div like this:

<div class="box-body">
   <a href="#" id="hideshow">Hide</a></br>
   <div id="message">
      <p>password: <input type="password" name="blog_pass"></p>
    </div>
</div>

Basically what I want to do is to hide the div message when I click on the hideshow link and show it again when I click on this link again.

So I added a script at the end of my page in order to do this:

$('#hideshow').toggle(function(){
    $('#hideshow').text('Show');
    $('#message').hide();
}, function(){
    $('#hideshow').text('Hide');
    $('#message').show();
});

But now the problem is it does not even work out. I mean no error appears on console and even the link Hide is not showing somehow.

So what is your idea about this... How can I fix this?

1 Answers1

0

You can use a class say, hidePwd to toggle it for hide/show operation:

$('#hideshow').click(function(){
    var newText = $('#hideshow').text().trim() === 'Hide'? 'Show':'Hide';
    $('#hideshow').text(newText);
    $('#message').toggle('hidePwd');
});
.hidePwd{
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box-body">
   <a href="#" id="hideshow">Hide</a><br>
   <div id="message">
      <p>password: <input type="password" name="blog_pass"></p>
    </div>
</div>
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62