Home > Software design >  how to get clicked Button attribute Id and value in jquery
how to get clicked Button attribute Id and value in jquery

Time:06-05

i wanna get button id and value when clicked by this situation

this is my html i write this html tags

<div >
    <div >
        <button id="btnadd" >add attribute</button>
        <br>
        <div  id="holder">
            <div id="mainholder1" >
                <div >
                    <input type="text"  name="title" placeholder="enter title">
                </div>
                <div >
                    <textarea name="desc" id="desc" ></textarea>
                </div>
                <div >
                    <button >remove</button>
                </div>
            </div>
        </div>
    </div>
</div>

and in my script file i added this and i just want access to the clicked button by calling function

 $(function () {
        var i = 1;
        $("#btnadd").click(function () {

            var elemnts =
                '<div  id="holder'   i     '">'  
                '<div >'  
                '<input type="text"  name="title" placeholder="enter title">'  
                '</div>'  
                '<div >'  
                '<textarea name="desc" id="desc" ></textarea>'  
                '</div>'  
                '<div >'  
                '<button id="hhs" onclick="hii()"  value="'   i   '">remove</button>'  
                '</div>'  
                '</div>';

            $("#holder").append(elemnts)
        })

    })

    function hii(){
        // i wanna get access to the clicked button
    }

CodePudding user response:

Abbas. Please try below.

$(function () {
    var i = 1;
    $("#btnadd").click(function () {

        var elemnts =
            '<div  id="holder'   i     '">'  
            '<div >'  
            '<input type="text"  name="title" placeholder="enter title">'  
            '</div>'  
            '<div >'  
            '<textarea name="desc" id="desc" ></textarea>'  
            '</div>'  
            '<div >'  
            '<button id="hhs"  value="'   i   '">remove</button>'  
            '</div>'  
            '</div>';

        $("#holder").append(elemnts)
    })
    $(document).on('click', '#hhs', function() {
        // hii function code should be here.
    });
});

Hope this helps. Happy coding~ :)

  • Related