How to add default value of field in NewForm with javascrip/JQuerry

Jakub Witoński 161 Reputation points
2020-11-20T14:02:12.417+00:00

Hi,
I want to make conditional behavior in newForm of list. It is external list connected to SQL Server. We are using SharePoint 2019 on Premise and I am using SharePoint Designer 2013 to edit forms.
I wanted to hide textbox and show other based on state of checkbox. I wanted to use javascript or JQuery, but I am stuck on simplest thing. For start I wanted to set default value of field at start of script. I couldn't make it work.
I think that maybe I don't know how to properly address it.

In newForm i got field:

<tr>  
 <td width="190px" valign="top" class="ms-formlabel">  
 <H3 class="ms-standardheader">  
 <nobr>NameOfField</nobr>  
 </H3>  
 </td>  
 <td width="400px" valign="top" class="ms-formbody">  
 <SharePoint:InputFormTextBox runat="server" id="ff7{$Pos}" Text="24" __designer:bind="{ddwrt:DataBind('i',concat('ff7',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@NameOfField')}"/>  
 <SharePoint:FieldDescription runat="server" id="ff7description{$Pos}" FieldName="NameOfField" ControlMode="New"/>  
 </td>  
</tr>  

And I was trying to change it with this code:

41394-tmp.jpg

(I have no idea why, but I can't pass js code here. I got errors when posting question)
To check if my code is executing I added console.log('test1') and I got message in browser console.

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,367 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,619 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,571 Reputation points
    2020-11-23T08:07:59.967+00:00

    Hi @Jakub Witoński ,

    Firstly, if you want to post some Jquery code in the forum, please upload with an txt attachment file so that it could avoid error when posting question.

    Back to the original question, please check the <select> element value using Developer Tool, the value "1234567890" should existed in <select>, please check the following code (in my sample below, the value is In-progress):

    41821-snipaste-2020-11-23-15-09-18.png

    41831-snipaste-2020-11-23-15-11-19.png

    If the field is a textbox field rather than <select> dropdown, then should use <input> tag like this:

    41841-snipaste-2020-11-23-16-05-59.png

    Here is the complete code snippet in attachment file, please check:

    41690-code.txt

    If the issue still existed, please post the screen capture in Developer Tool for the field, so that we can research and find the solution for you.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. sadomovalex 3,631 Reputation points
    2020-11-20T15:51:38.177+00:00

    few suggestions:

    1. check in browser developer tools (F12) > Network tab that jquery-3.5.1.min.js is downloaded correctly. If not try to use it from public CDN for testing
    2. add console.log("test2") under $(document).ready
      $(document).ready(function() {
          console.log("test2");
      });
      

    if it won't be called use jQuery instead of $ symbol:

    jQuery(document).ready(function() {
        console.log("test2");
    });
    
    1 person found this answer helpful.
    0 comments No comments

  2. Jakub Witoński 161 Reputation points
    2020-11-23T14:01:38.363+00:00

    Thank you, @Jerryzy-MSFT
    Now I understand. I should use same tag as in html ("input", not "select"). Based on different examples and my experience from SQL I was thinking that with "select" I am... selecting something :). And with title I was also wrong.

    Thank you again.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.