0

I've written a function for displaying 2 text-boxes when user clicks a button. But the issue is when click these 2 buttons an event for another button in the form is fired. So I wrote a mouse click event for that Hide and Show functions. But still it's not working. When the show button clicks 2 text-boxes are showing and when hide clicks they disappear. I'll put my coding down below.

     <div class="form-group">
            <div class="col-sm-9">
              <input type="text" id="height"/>
                <input type="text" id="width"/>

                <button id="hide">Hide</button>
                <button id="show">Show</button>


        </div>
    </div>

$("show").click(function(){ #Mouse Click event for show button


  $(document).ready(function(){
     $("#input1").hide();
             $("#input2").hide();
        $("#hide").click(function(){
            $("#input1").hide();
             $("#input2").hide();
        });
        $("#show").click(function(){
            $("#input1").show();
             $("#input2").show();
        });
    });

    });
1
  • 1
    The element IDs in your JS don't match your HTML. Also, it doesn't make sense to put a document ready handler inside a click handler. Also $("show") is looking for a <show> element, you need a # as in your other code. Commented Sep 27, 2017 at 3:02

2 Answers 2

1

Try this

  $(document).ready(function() {
    $("#height, #width").hide();
    $("#hide").click(function() {
      $("#height, #width").hide();
    });
    $("#show").click(function() {
      $("#height, #width").show();
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <div class="col-sm-9">
    <input type="text" id="height" />
    <input type="text" id="width" />

    <button type="button" id="hide">Hide</button>
    <button type="button" id="show">Show</button>


  </div>
</div>

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. But the event for the cart function still firing. When I click show or hide button it still navigate to the cart because there is a button below called "Add to cart". Do you have any idea how to stop that happening? @Shadow Fiend
@RyanOscar add this.<button type="button">.. its because if the button is inside a form it still acts as submit.. you need to specify it type first.. I edited my answer..
Now it's not redirecting to the cart page. But there's another catch now. Show and Hide buttons are not working now. It supposed to show the 2 text-boxes when click show and hide when click hide. @Shadow Fiend
@RyanOscar can you post your complete html and js so i can find what is the problem.. or just the relevant code that are affecting the function..
@RyanOscar the click function is not working because you've got wrong selector.. your id's are height and width not input1 and input 2.. change your jquery with the jquery I made in the answer and it should work..
|
0

Just call click function inside document.ready. Also trigger events on proper element using appropriate selectors

$(document).ready(function() {

  $("#hide").click(function() {
    $("#height ,#width").hide();

  });
  $("#show").click(function() {
    $("#height ,#width").show();

  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
  <div class="col-sm-9">
    <input type="text" id="height" />
    <input type="text" id="width" />

    <button id="hide">Hide</button>
    <button id="show">Show</button>


  </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.