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.

5 Answers 5

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
  • +1 That did it. I added jQuery to the page and this syntax worked. Thanks. Commented Sep 26, 2013 at 14:03
2
$("select[title*='Time Period'] option:selected").text();
2
  • Can you please also provide some explanation of the code.
    – Asad Refai
    Commented Apr 21, 2016 at 9:09
  • thx, works for dropdowns.
    – Popkornak
    Commented Jan 25, 2020 at 15:49
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]');
1
  • Don't forget you can flag posts as duplicates, although in this case it is on SO.
    – SPDoctor
    Commented Sep 26, 2013 at 7:49
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 and acknowledge you have read our privacy policy.

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