2

I have a form field:

<SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="New" FieldName="Number_x0020_1" __designer:bind="{ddwrt:DataBind('i',concat('ff2',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Number_x0020_1')}"/>

I'd like to get the value of it in my javascript function. I was able to find it at the time of rending in html, but I know there's a better way to do this, but try as I might in the search I can't find it. Here's my solution right now.

var my_input1 = document.getElementById('ctl00_m_g_bb2353f3_193b_4566_86ff_0c4dd6671dfc_ff21_ctl00_ctl00_TextField').value;

I'm just using SharePoint Designer for this, and I don't want to adjust the web.config file to allow for server scripting if I can avoid it.

6

I guess the easiest way is to use jquery. You just have to refer jquery in your form from designer and then you can fetch the field from title or name attribute whichever is applicable.

I guess this is the input text field so your jquery will be like

$("input[title='title_of_the_field']").val()

You can see the title in developer toolbar from where you get the id.

Hope this helps. For more info refer http://jquery.com/

  • +1 That did it. I added jQuery to the page and this syntax worked. Thanks. – J. Simonsen Sep 26 '13 at 14:03
1

You could wrap the FormField in a span tag with an Id that you know and then you use querySelector to get the value (assuming you don't need to support below IE8).

<span id='myid'>
    <SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="New" FieldName="Number_x0020_1" __designer:bind="{ddwrt:DataBind('i',concat('ff2',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Number_x0020_1')}"/>
</span>

var my_input1 = document.querySelector("span#myid input").value;

At least this way you wouldn't be relying on the .NET generated ID.

1

Similar question to yours has already been answered on StackOverflow: Javascript to get sharepoint form field value to a variable.

You have to append "Field" to the field name; e.g. for Title (using jQuery):

var $titleField = $('input[id*=TitleField]');
  • Don't forget you can flag posts as duplicates, although in this case it is on SO. – SPDoctor Sep 26 '13 at 7:49
1
$("select[title*='Time Period'] option:selected").text();
  • Can you please also provide some explanation of the code. – Asad Refai Apr 21 '16 at 9:09
0

I don't know if you are trying to alter an existing lists' Form page or having your own, but you can get ClientID in the aspx page by using the example below. However I'm not sure how asp.net handles IDs using special characters.

var my_input1 = document.getElementById("<%= ff2{$Pos}.ClientID%>");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.