0

I am using a content editor webpart and jQuery to disable fields in the edit form of a task. it works when I add the code directly in to the content editor webpart but when I add the js file to site assets and link it to the webpart it does not work. does anyone know what I am doing wrong? sorry for the simple question i am new to jQuery. here is my code:

<script src="/SiteAssets/jquery-3.1.0.min.js" type="text/javascript"> </script><script type="text/javascript">
$(function() { 
    $("input[Title='Task Name']").attr('disabled', 'disabled'); 
    $("select[Title='Content Type']").attr('disabled', 'disabled'); 
});
</script>​​
  • Check your console for error logs, you'll have a hint there. Most probably it's the jquery pathname. Try referring jquery from cdn. – Aveenav Oct 20 '16 at 16:28
  • @Henry What URL are you using for the Content Link? It has to reference an HTML file, not a JavaScript file – Denis Molodtsov Oct 20 '16 at 17:19
1

Where are you storing this js file? According to your code, you're storing your jquery library in Site Assets, so if you're storing your js code in there as well, you need to remove that part of your path as so:

    <script src="jquery-3.1.0.min.js" type="text/javascript"> </script><script type="text/javascript">
$(function() { 
    $("input[Title='Task Name']").attr('disabled', 'disabled'); 
    $("select[Title='Content Type']").attr('disabled', 'disabled'); 
});
</script>​​
  • 1
    Wow that totally makes sense why it was working on way and not the other. such a dumb mistake. thanks so much! – Henry Oct 20 '16 at 16:39
  • 1
    src="jquery-3.1.0.min.js" is the path relative to the Current Page where the CEWP is located – Denis Molodtsov Oct 20 '16 at 17:14
  • Not if he's loading the script via the Content Link within the CEWP. Since the script he's loading is stored in the same location as the jquery library, the 'SiteAssets' path wouldn't need to be included. – Kelvin Hoyle Oct 20 '16 at 17:41
  • I've just tried it. I does not work this way. The URL you are using is Relative to the Current Page, not to the HTML file you are referring. Either you are mixing it up or I misunderstand what you mean. Here is the screenshot with the proof that it does not really work i.imgur.com/wQUCvN2.png – Denis Molodtsov Oct 21 '16 at 18:13
1

The content editor will only allow for .js files to work when they are linked as .txt files. Weird right?? Save the .js file as a .txt file and then you can use it as a linked file from the Content Editor Web Part. What you may also want to do is use the "Script Editor" web part. This will allow direct insertion of .js code. I actually prefer the Script editor when you're not rendering dom elements from your script.

  • 1
    +1 If you want to load JS files you could also use JSLink or Site/Web UserCustomActions (ScriptLink) – Danny '365CSI' Engelman Oct 20 '16 at 16:44
  • I tried to rename the .js file to .txt. It didn't work for me in SharePoint Online. CEWP just renders file content on the screen – Denis Molodtsov Oct 20 '16 at 17:02
0

I could be terribly wrong, but I don't think you can use CEWP to reference the JavaScript files.

Update:

What I mean is that you cannot use CEWP property to reference files with .js extension. Obviously, you can reference .HTML files that have references to JavaScript files.

Update 2

Hopefully, this image will make it clear:

enter image description here

  • From my understanding, we can very well use CEWP to reference JavaScript. You have to write your code within <script type="text/javascript"></script>. I have already used this way and it is working fine for me. However I don't know which one is efficient way. – Rohit Waghela Oct 20 '16 at 16:25
  • Oh, sure, but it's an HTML file you are referencing, not the JavaScript directly – Denis Molodtsov Oct 20 '16 at 16:56
0

I had a similar issue where JS script wasn't doing anything and have not encountered any errors in the console. I ended up using Script editor webpart or could also embed script directly as show below.

NB: this occurred in SharePoint Online

enter image description here

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.