开发者

Get Id of an dynamically created javascript element

开发者 https://www.devze.com 2023-03-31 20:03 出处:网络
I have a list which is dynamically created like this: inner += \"<li class=\'linkstyl\'> <a href=\'#\"+tot+\"\' id=\'a\"+tot

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_a开发者_运维知识库ddress+"</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.

0

精彩评论

暂无评论...
验证码 换一张
取 消