Skip to content Skip to sidebar Skip to footer

Can Not Find Element Or Element Id That Is Added With Php

using jquery i am adding new elements to a div element i also have set id of new added element but still javascript can not find that newly added elements. $.ajax({ //ajax call

Solution 1:

jQuery is only aware of the elements in the page at the time it runs, so new elements added to the DOM are unrecognized by jQuery. To combat the problem use event delegation, bubbling events from newly added items up to a point in the DOM which was there when jQuery ran on page load. Many people use document as the place to catch the bubbled event, but it isn't necessary to go all the way up the DOM tree. Ideally you should delegate to the nearest parent existing at the time of page load.

In addition, ID's Must Be Unique, specifically because it will cause problems in JavaScript and CSS when you try to interact with those elements.

Change your selectors (switching from jQuery to vanilla JS is really not necessary since you're already using jQuery) to classes and use on() to perform the event delegation:

$(document).on('click', '.validator', (function (){//validator is my submit button id
    var value = document.getElementsByClassName('select_division'); //select_division is id of my new added element
    alert(value);
})

Part of the problem here is you will want a specific select_division related to your validator. In order to get that you may to perform some DOM traversal. Without seeing your markup I cannot tell you how to do the traversal.


Solution 2:

problem solved i was writing wrong. it can not be done with

$("#elementid").click(function (){//v

it only works with

var value = document.getElementById('elementid').value;

thanks for helping


Post a Comment for "Can Not Find Element Or Element Id That Is Added With Php"