locked
where to put javascript code? RRS feed

  • Question

  • Hello,

    I am trying to set some columns in a list as "read-only" and is using the following code:

    <script type=”text/javascript”>

    function SetReadOnly()

    {

    var elements=document.getElementById(’4_ctl00_ctl00_TextField’);

    elements.readOnly=true;

    }

    _spBodyOnLoadFunctionNames.push(“SetReadOnly()”);

    </script>

    But I am not sure where to put the code in. Should I put it in the space in Content Editor Web Part, or through a link to a txt file, or in "Edit HTML"? I've tried them but none works.

    Thanks a lot!

    Patrick

    Wednesday, February 26, 2014 3:11 PM

Answers

  • You can try this:

    1) Open your Sharepoint List. Go to List edit view.

    2) On right side of Ribbon you will find "Form Web Parts" option as shown in figure.

    3) Choose your List form which you want to edit.

    4) Now you can add web part in new window.

    5) Add Content Editor Web part.

    6) In content editor web part add the path of your "txt" file in which you have written your script, for eg.

    <!DOCTYPE html>
    <html>
    <body>
    <script type=”text/javascript”>
    function SetReadOnly()
    {
    var elements=document.getElementById('4_ctl00_ctl00_TextField');
    elements.readOnly=true;
    }
    _spBodyOnLoadFunctionNames.push("SetReadOnly()");
    </script>
    </body>
    </html> 
     

    I haven't tried this method so I am not sure but hope it works...:D

    --------------------------------------------------------------

    ***If my post is answer for your query please mark as answer***

    ***If my answer is helpful please vote***



    Wednesday, February 26, 2014 3:58 PM
  • Hello,

    Simply put your script in content editor webpart. If you want to set on newform.aspx page then open this page and add webpart. If you want to apply same for editform.aspx page as well then you need to do same for edit page also.

    To open newform-->go to you list and type http://sitename/Lists/listname/newform.aspx-->then click edit page from site action-->now add CEWP and put script in HTML source.

    To open editform--http://sitename/Lists/Test1/editform.aspx-->then edit page and do same.

    No need of designer and customize your page.

    Hope it could help


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Dennis Guo Friday, March 7, 2014 5:34 PM
    Thursday, February 27, 2014 10:15 AM

All replies

  • Hi

    just after PleHolderMain element

    <asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
    
    
    
    <script type="text/javascript" >
    



    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, February 26, 2014 3:15 PM
  • Hi romeo,

    is that something in sharepoint designer? sorry but I'm not using SP designer due to company policy; so where should I put my code in then?

    thanks!

    Wednesday, February 26, 2014 3:22 PM
  • hi

    yes, you should open the page with Sharepoint Designer. Insert there you script and that's it!


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, February 26, 2014 3:28 PM
  • i mean i cant use sp designer due to company policy...
    Wednesday, February 26, 2014 3:30 PM
  • You can try this:

    1) Open your Sharepoint List. Go to List edit view.

    2) On right side of Ribbon you will find "Form Web Parts" option as shown in figure.

    3) Choose your List form which you want to edit.

    4) Now you can add web part in new window.

    5) Add Content Editor Web part.

    6) In content editor web part add the path of your "txt" file in which you have written your script, for eg.

    <!DOCTYPE html>
    <html>
    <body>
    <script type=”text/javascript”>
    function SetReadOnly()
    {
    var elements=document.getElementById('4_ctl00_ctl00_TextField');
    elements.readOnly=true;
    }
    _spBodyOnLoadFunctionNames.push("SetReadOnly()");
    </script>
    </body>
    </html> 
     

    I haven't tried this method so I am not sure but hope it works...:D

    --------------------------------------------------------------

    ***If my post is answer for your query please mark as answer***

    ***If my answer is helpful please vote***



    Wednesday, February 26, 2014 3:58 PM
  • Hello,

    Simply put your script in content editor webpart. If you want to set on newform.aspx page then open this page and add webpart. If you want to apply same for editform.aspx page as well then you need to do same for edit page also.

    To open newform-->go to you list and type http://sitename/Lists/listname/newform.aspx-->then click edit page from site action-->now add CEWP and put script in HTML source.

    To open editform--http://sitename/Lists/Test1/editform.aspx-->then edit page and do same.

    No need of designer and customize your page.

    Hope it could help


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    • Marked as answer by Dennis Guo Friday, March 7, 2014 5:34 PM
    Thursday, February 27, 2014 10:15 AM