tag:blogger.com,1999:blog-4721830145069718578.post5805101796820308520..comments2023-04-25T15:15:17.530+10:00Comments on SharePoint Development & Administration + InfoPath: Set SharePoint List Form Web Part field values using JavaScript when Creating New ItemDanielShttp://www.blogger.com/profile/07274977002584498975noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-4721830145069718578.post-67899413318804945832010-09-24T19:18:19.305+10:002010-09-24T19:18:19.305+10:00I should also note that if you did want to have th...I should also note that if you did want to have the JavaScript in the head section of the page, included in the master page or in a separate JavaScript file, you could do so by including the script in a function instead, which would then then called after the List Form has loaded on the page.<br /><br />If including the function in a separate JavaScript file linked to the page, the function would also need to be called after the form loads (using a content editor web part, or by including the script in the page source after the web part zone that contains the List Form).<br /><br />Use a JavaScript debugger to check that your script is being executed on the page, and is working as expected, and for any syntax errors. Displaying alerts containing the data which has/is being processed can also help to follow the process, and ensure that the script is being executed and completed without (logical or syntax) errors.<br /><br />I use <a href="http://getfirebug.com/" rel="nofollow">Firebug</a> (Firefox plugin) mostly for debugging JavaScript (also good for adjusting html and CSS on-the-fly when troubleshooting layout and style issues in Firefox).DanielShttps://www.blogger.com/profile/07274977002584498975noreply@blogger.comtag:blogger.com,1999:blog-4721830145069718578.post-14726251364818169642010-09-24T10:34:41.600+10:002010-09-24T10:34:41.600+10:00Hi Parth,
The issue is most likely due to the pos...Hi Parth,<br /><br />The issue is most likely due to the position on the page that the script is executed. I tested this by adding the script to the head section using the asp:content AdditionalPageHead element without success.<br /><br />My solution was to add a content editor web part to the page (under the list form web part), which contains the javascript to update the requitred field. Include the following in the content editor web part:<br /><br /><br /><script type="text/javascript" language="javascript"><br /><!--<br />var txtID = document.getElementById('ctl00_m_g_02afb0bf_880c_4a67_bbd3_ab97c94a902d_ctl00_ctl04_ctl17_ctl00_ctl00_ctl04_ctl00_ctl00_TextField');<br />txtID.value = '9';<br />//--><br /></script><br /><br />ThanksDanielShttps://www.blogger.com/profile/07274977002584498975noreply@blogger.comtag:blogger.com,1999:blog-4721830145069718578.post-73540309249975666422010-09-24T00:53:02.009+10:002010-09-24T00:53:02.009+10:00Pardon my ignorance please, but this is what i was...Pardon my ignorance please, but this is what i was able to write :<br /><br /><br />var txtID = document.getElementById('ctl00_m_g_02afb0bf_880c_4a67_bbd3_ab97c94a902d_ctl00_ctl04_ctl17_ctl00_ctl00_ctl04_ctl00_ctl00_TextField');<br />txtID.value='9';<br /><br />Please let me know where i am going wrong ??parthhttps://www.blogger.com/profile/02901295259866272396noreply@blogger.comtag:blogger.com,1999:blog-4721830145069718578.post-5723988738525028682010-09-23T23:03:46.747+10:002010-09-23T23:03:46.747+10:00Hi Parth,
The JavaScript sets the value of a fiel...Hi Parth,<br /><br />The JavaScript sets the value of a field in the NewForm to something that is known, or a value that can be retrieved from a query string parameter or similar. <br /><br />You will need to view the source code of the NewForm.aspx page, then find the html code for the input field you want to set the value of. Once you have found the input field, copy the value in the id attribute of the input element and paste into the string passed to the getElementById() function.<br /><br />txtID = document.getElementById('field_id');<br /><br />You would then set the value of the field:<br />txtID.value = 'xxxxxxxxxx';<br /><br />The javascript in the article above sets the value of a multiple choice field, by first un-checking the default option, the checking the option that is required. In your case you would need to set the value of the text field using the javascript instead.<br /><br />In the case of the article above, the required value is determined and set using audiences and targeting.<br /><br />ThanksDanielShttps://www.blogger.com/profile/07274977002584498975noreply@blogger.comtag:blogger.com,1999:blog-4721830145069718578.post-56963544647552675392010-09-23T22:45:07.509+10:002010-09-23T22:45:07.509+10:00I am not able to do this..can you elaborate more ?...I am not able to do this..can you elaborate more ???<br /><br />I have Newform and Number which i want to set<br />Fieldname = CustomerID<br />Thanks<br />Parthparthhttps://www.blogger.com/profile/02901295259866272396noreply@blogger.com