JavaScript to set default value on SharePoint form field

Matt Pinkston
Matt Pinkston used Ask the Experts™
on
I found the below script, placed it in but no change...  is there any obvious issue?

<script src=”https://code.jquery.com/jquery-1.11.0.min.js”</script>
<script type=”text/javascript”>

$(document).ready(function() {

$(“select[Title=’PursuitType’]”).val(‘CLIN 6AG’);

});

</script>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Works for me - so either you don't have a select with that title or there is some error on your page.

1. Check your console for errors (F12)
2. Check that you have a select with Title PursuitType

This sample demonstrates the code works

Aside: why are you using a title as a selector? Surely the <select> has at least a name or an id - which would be more logical?
Matt PinkstonEnterprise Architect

Author

Commented:
Thanks for the reply

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

When I try to add title= it says that is not valid
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Ok but I ask again why are you trying to reference the select by title?

Why not ID or Class?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Matt PinkstonEnterprise Architect

Author

Commented:
the JavaScript I found asks for title????

$(“select[Title=’PursuitType’]”).val(‘CLIN 6AG’);
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
the JavaScript I found asks for title????
The code you posted has an ID so you can use an id

$("#ff2x{$Pos}").val('CLIN 6AG'); 

Open in new window


If there is only one select on the page then

$("select").val('CLIN 6AG'); 

Open in new window


Failing that post the rendered HTML for the page and let's see what SharePoint spits out.
Matt PinkstonEnterprise Architect

Author

Commented:
Thanks....
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial