• How do you access the DOM node in jquery inside a javascript variable?

    I have the following html

    <div id="list_item_template"><li><a href="#">Text</a></li></div>
    

    and javascript:

    var item = $("#list_item_template").clone();
    

    I want to access the internal tag of the <a>cloned copy and add the attribute. Without cloning, I would simply do:

    $("#list_item_template a").attr("onclick", "SomeFunction()");
    

    However, I need to perform this operation on the cloned copy, and not on the html located on this page. How should I do it?

    +3
    source share
    2 answers

    item.find('a'); must do it.

    +3
    source
    $("#list_item_template a").attr("onclick", "SomeFunction()");
    

    not recommended ... read this ...

    use .click()instead ...

    $("a",item).click(SomeFunction);
    
    +2
    source

    Source: https://habr.com/ru/post/1745393/


    All Articles