Jquery changes on element which is added dynamically is not working

Moenie

Adding HTML dynamically to a container element. the dynamically added HTMl has a element which have an onclick attribuut. the onclick function of this element will be used to show a div element which is also added dynamically.

the last point is not working if i click on the element the function will be call. but the element isn't shown. its looks like that the browser don't redraw the changes.

Do somebody know what i am doing wrong??

sorry for my bad english if something is not clear please ask me.

see the sample code here

<body>
    <div>
        <span onclick="showSubjectContentInContainer('subjectContent')">Subject description</span>

        <div id="subjectContent" style="display: none;">
                <div >
                    <div>
                        [CONTENT]
                    </div>
                    <div class="showSubContent" onclick="showSubContent('subContent')">Click here to show Sub Content</div>
                    <div id="subContent" style="display: none;">
                        [SUB-CONTENT]
                    </div>
                </div>
        </div>
    </div>
    <div id="container"></div>

    <script type="text/javascript">
        function showSubjectContentInContainer(subjectContentId)
        {
            //get inner html of the subjectContent and place it in the Container div
            var htmlSubjectContent = $("#" + subjectContentId).html();
            $('#container').html(htmlSubjectContent); //THIS WORK FINE
        }

        function showSubContent(subContentId)
        {
            //show the subContent which is added dynamically
            $("#"+subContentId).show(); //THIS NOT WORKING...!!
        }
    </script>
</body>  

hahaha

EDIT: I think I found the problem. $("#subContentC").show(); points to the first <div id="subContent".. element and does .show() to that element instead of the dynamically created one.

Possible fixes.

1) working fiddle here Basically instead of selecting the element with id (which will only select one) select it with class (so it runs .show() on all elements with the specific class)

$("."+subContentId).show();


2) working fiddle here After creating the container (content and subcontent) delete the subjectContent div so id-naming wont clash any more

$( "#subjectContent" ).remove();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery - Dynamically added element not working on mouseleave, click events

From Dev

Jquery Dynamically added class Not working

From Dev

Working with dynamically added elements in jquery

From Dev

Jquery not working on dynamically added button?

From Dev

Jquery Dynamically added class Not working

From Dev

JQuery Selector on Dynamically Added Element

From Dev

jQuery click on a dynamically added element

From Dev

Dynamically added element function call not working

From Dev

How to add events for dynamically added single element which shares the class names in JQuery?

From Dev

Jquery Droppable not working for Dynamically added DIVS

From Dev

tab not working when dynamically added using jquery

From Dev

Jquery Events Not Working For Dynamically Added Divs

From Dev

Jquery AutoComplete is not working in dynamically added form fields

From Dev

jquery not working when elements added dynamically

From Dev

Selectric not working when added dynamically using jquery

From Dev

jQuery click event not working for dynamically added elements

From Dev

Jquery Events Not Working For Dynamically Added Divs

From Dev

product/add of dynamically added inputs not working jquery

From Dev

Jquery click does not working with dynamically added link

From Dev

how to use jquery .data with dynamically added element

From Dev

jQuery on blur / focusout not firing on dynamically added element

From Dev

jQuery UI droppable on dynamically added element?

From Dev

jQuery select element with class added dynamically

From Dev

Jquery selector not behaving dynamically on added dom element

From Dev

jQuery on blur / focusout not firing on dynamically added element

From Dev

jQuery UI droppable on dynamically added element?

From Dev

How to make the DOM element stay(after refresh), which was added dynamically

From Dev

Dynamically added element will not delete

From Dev

JQuery - Dynamically created element css not working

Related Related

  1. 1

    jQuery - Dynamically added element not working on mouseleave, click events

  2. 2

    Jquery Dynamically added class Not working

  3. 3

    Working with dynamically added elements in jquery

  4. 4

    Jquery not working on dynamically added button?

  5. 5

    Jquery Dynamically added class Not working

  6. 6

    JQuery Selector on Dynamically Added Element

  7. 7

    jQuery click on a dynamically added element

  8. 8

    Dynamically added element function call not working

  9. 9

    How to add events for dynamically added single element which shares the class names in JQuery?

  10. 10

    Jquery Droppable not working for Dynamically added DIVS

  11. 11

    tab not working when dynamically added using jquery

  12. 12

    Jquery Events Not Working For Dynamically Added Divs

  13. 13

    Jquery AutoComplete is not working in dynamically added form fields

  14. 14

    jquery not working when elements added dynamically

  15. 15

    Selectric not working when added dynamically using jquery

  16. 16

    jQuery click event not working for dynamically added elements

  17. 17

    Jquery Events Not Working For Dynamically Added Divs

  18. 18

    product/add of dynamically added inputs not working jquery

  19. 19

    Jquery click does not working with dynamically added link

  20. 20

    how to use jquery .data with dynamically added element

  21. 21

    jQuery on blur / focusout not firing on dynamically added element

  22. 22

    jQuery UI droppable on dynamically added element?

  23. 23

    jQuery select element with class added dynamically

  24. 24

    Jquery selector not behaving dynamically on added dom element

  25. 25

    jQuery on blur / focusout not firing on dynamically added element

  26. 26

    jQuery UI droppable on dynamically added element?

  27. 27

    How to make the DOM element stay(after refresh), which was added dynamically

  28. 28

    Dynamically added element will not delete

  29. 29

    JQuery - Dynamically created element css not working

HotTag

Archive