How to access a DIV from javascript if ASP.NET manages its identifier?

I have a webpage containing a div element. On the page there are links to javascript on div: document.getElementById('divId'). This worked fine until another developer redesigned the page to use the ASP master page.

Now document.getElementById('divId')returns null. It looks like ASP.net adds some characters to the names of elements in content forms when you use the main page. How do I know what a div identifier means when a page loads?

Refresh Let me give you a concrete example to clarify the question: there was a div with an identifier on my page divNotice. After changing my page to use the main page, I see when I print the source on a page that displays the div id is equal ctl00_ContentPlaceHolder1_divNotice. My question is, how should I know what will happen to the div id when the wireframe is done with it?

+3
source share
4 answers

I think this is what you are looking for.

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

to get the id of your element that appears on the html page, use .ClientID

I hope for this help.

+10
source

javascript, Control.ClientID, div.

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

, . . : ASP.NET jQuery

, / , .

+2

, , , null

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

:

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

, .. - :

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")
    }
}

: http://jsfiddle.net/pnHSw/2/

, .

JS-, ASP.net

edit: , Aristos : P

+1

maybe you can use un css descendants selector

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

wrapperControler controler {

 dosomething;

}

0
source

Source: https://habr.com/ru/post/1750174/


All Articles