Title: Controls in ContentPlaceHolder are not recognized by javascript document.getelementsbyname
Tags: javascript;asp.net;master-pages;contentplaceholder;getelementsbyname
Question: I have a calender script in my project. For using it, I have added an image and on its onclick event I call a javascript function displayDatePicker:
<img alt="Calender" src="calender.jpg" onclick="displayDatePicker('txtBox1', this);"/&gt;
I use a masterpage in my project and if the image and the associated textbox control are in contentPlaceHolder, the javasript library's function document.getElementsByName returns null and it seems that it can not detect controls inside a contentPlaceHolder.
I used this code for a page that was not using a masterpage and everything went well.
I appreciate any help from you in advance.
Good luck.
Comment: this is because the client id are different after rendering. http://stackoverflow.com/questions/1362338/how-do-i-pass-a-server-controls-actual-client-id-to-a-javascript-functioniew


Here is another answer: Your asp textbox may render as something like:
<input name="ctl00$FeaturedContent$txtBox1" id="FeaturedContent_txtBox1" type="text"/&gt;
So, in javascript, you should use clientid:
<img alt="Calender" src="calender.jpg" onclick="displayDatePicker('<%= txtBox1.ClientID %&gt;', this);"/&gt;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:08/13/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><asp.net><master-pages><contentplaceholder><getelementsbyname>', 'Title': 'Controls in ContentPlaceHolder are not recognized by javascript document.getelementsbyname', 'ViewCount': 1018, 'id': 18203629, 'user_AboutMe': None, 'user_AccountId': 2846279.0, 'user_DisplayName': 'ali nasri', 'user_Reputation': 19.0, 'user_Views': 7.0, 'user_id': 2444578.0}}