开发者

Problem with button click event in jquery

开发者 https://www.devze.com 2023-04-01 05:55 出处:网络
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\">

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 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消