6

I am using jQuery on button click to show div but don't know why its not working...

HTML:

<input type="button" id="addmoresg" value="Add More" name="button">
<div id="addsg" style="display:none">
    <!-- more HTML here -->
</div>

JavaScript:

$(document).ready(function() {
    $('.addmoresg').click(function() {
        $('.addsg').show("slow");
    });
});

jsFiddle demo: http://jsfiddle.net/XGVp3/

I am not getting any result on button click.

2 Answers 2

8

2 problems:

  1. You did not select jQuery as library in your demo.
  2. You use class selectors [docs] (.addmoresg) instead of id selectors [docs] (#addmoresg). Your elements only have ids, not classes:

    <input type="button" id="addmoresg" value="Add More" name="button"> 
    

    $('.addmoresg) would select elements with class="addmoresg", e.g.

    <input type="button" class="addmoresg" value="Add More" name="button"> 
    

Working demo

jQuery has a great documentation and a list of all possible selectors, with examples.

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

1 Comment

I knew about that both the we should have selector. and for id we use # and for class we use dot. but same was not working in morning... Still also I have just remove comments in my previous code and its working..
0

just change your code as:

$(document).ready(function() {
    $('#addmoresg').click(function() {
        $('#addsg').show("slow");
    });
});

Basically, you were targeting the class adddsg (done by a .class). Since the div has and ID of adddsg, you need to target using #ID

Hope that helps.

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.