简体   繁体   中英

Get Id of an dynamically created javascript element

I have a list which is dynamically created like this:

inner += "<li class='linkstyl'> <a href='#"+tot+"' id='a"+tot
         + "' style='text-decoration:none'  onclick='addpoint("
         + results[0].geometry.location.lat()+","
         + results[0].geometry.location.lng()+")'> " 
         + results[0].formatted_address+"</a></li>"

Then I am using:

$("#showaddress").html(inner);  // to insert it inside <ul> with id as  showaddress .

My problems is that when I try to getElementById of the this dynamically created elements it returns me a null value.
How do I reference this dynamically created elements?

That code works fine. Perhaps you are trying to use getElementById() before the ajax request completes. At that point the elements won't be on the page still. Otherwise perhaps your tot variable doesn't contain what you think it does.

Try checking those two things over.

javascript is asynchronous. May be the elements are not yet available when you are trying to get them. Try the selector in the callback itself where you get the response from the geocoder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM