How do I access a DIV from javascript, if ASP.NET mangles its ID?

自古美人都是妖i 提交于 2019-12-06 00:33:06

I think that this is what you looking for.

document.getElementById('<%=divNotice.ClientID%>')

to get the ID of your element as appears on the html page use .ClientID

Hope this help.

Dynamically create the javascript using Control.ClientID to determine the calculated ID of div.

document.getElementById('<%= DivControl.ClientID %>')

Or search for the element on the client side using the base ID as a search pattern. See here: A generic way to find ASP.NET ClientIDs with jQuery

I prefer the server side calculation, but if you don't do it often and/or your current design prohibits it, the client side way is a reasonable workaround.

you can check i the element exists by checking if it returns not null

if (document.getElementById('divId') != null) { /* do your stuff*/ }

in other words:

if (document.getElementById('divId')) { /* do your stuff*/ }

now you have edited you orginal question i got it.. i would do something like this:

var arrDivs = document.getElementsByTagName('div'),
    strDivName = "divId";

for (i=0;i<=arrDivs.length;i++){
    if( arrDivs[i].id.indexOf(strDivName) != -1) {
        alert("this is it")
    }
}

you can see a demo here: http://jsfiddle.net/pnHSw/2/

i think you could do it better with a regex.

But this is a pure JS way i don't know ASP.net

edit: i think Aristos solution is much cleaner :P

maybe you can use a descendent selector un css

<div id="wrapperControler">
    <controler id="controler"></controler>
</div>

wrapperControler controler{

 dosomething;

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!