Adding jQuery to a SharePoint Page: Step One, Always
No matter how many times I add jQuery to a page in SharePoint, this is absolutely *always* the first thing that I do:
<script language="javascript" type="text/javascript" src="/Scripts/jquery-1.5.min.js"></script> <script language="javascript" type="text/javascript" src="/Scripts/jquery.SPServices-0.6.1.min.js"></script> <script language="javascript" type="text/javascript"> $(document).ready(function() { alert("jQuery"); }); </script>
If that alert in line 7 which is wrapped in a $(document).ready
fires, then my script references are good. I can’t tell you how many times I’ve talked people through that simple step. It even hangs me up, so it’s always good to do that alert first. (Sure, the alert can say “Bob’s your Uncle” or “Strawberry Jam”, but alert *something*.)
Thanks for your suggestion. The workflow did the trick. Appreciate you taking the time to respond
I am adding new Newform.aspx and tryiung to use jquery to show/hide fields. I dont know which js files to use. want to avoid cdn. using “jquery-1.10.2.js” from above link but not getting alert. please advise
If you don’t want to use a CDN, just store the .js file in a Document Library and reference it there.
M.
Hey Marc,
I have to make 2 single line text fields to be mandatory after selecting a value from (Status) dropdown list. Could you please provide me a jquery to make the work done
No, I can’t send you the exact jQuery. Have you tried anything yet?
M.
Please find the Jquery which I have tried.
//status is the dropdown list name
$(‘Status’).change(function () {
if (this.value == ‘Review’) {
$(‘Cause’).css(‘display’, ‘block’);
}
else {
$(‘Cause’).css(‘display’, ‘none’);
}
});
KAD:
Your selectors don’t make much sense. You need to use a DOM inspector like Firebug to look at the elements you want to select. for instance, rather than $(‘Status’), you probably need something like $(“select[Title=’Status’]”), assuming you have < 20 options for Status M.
Hey Mark, long time follower and used the trusty SP services in the past. Having the most simplest of issues though testing the alert, it ain’t showing up. Added the source under the PlaceHolder and made sure the reference is correct (to a local doc library) – I can click the js files and they open up fine in SPD but no go on the alert. Tried the new 2013.01 and even reverted back to the 7.1a and still nothing… its driving me nuts something so simple, yet it aint coming up!
Never mind – was obviously user error (had 2 references to the SPServices.js file) … haven’t slept much the past few days :/
Just wanted to say great information and thank you for the work you put into SP Services and your blog Marc!
Thanks Again!
Dan
You’re welcome, Dan! Glad you find it all helpful.
M.
IM new and just started with the request to populate user data on a form I read your instructions as requested and FAQ’s,
I’m using SP Designer 2013 using the recommended route to add the code to the page I get the following error
“Unknown server tag. Correlation ID: ee049f9c-d0ed-1000-69ef-a3b8b288aab5. ”
So I went back and tried to add your section on the first thing you add to every page to get the listed error.
I think I’m adding the code to the wrong place on the page. where should I add the code listed in the article?
DEW:
The best approach is to put the code into a file and reference that file in the Content Link in a Content Editor Web Part (CEWP).
M.
Hi,
I never had this issue in SP 2007 and SP 2010. But When I am trying to add the reference in SP 2013 List NewForm.aspx it is not firing. Any idea why? Please suggest me on this. Thanks in advance !.
–
Rao
Rao:
Hard to say. Probably best to post more details over on the SPServices discussions.
M.
Need a help on shArepoint button click event function