How to check if the user was clicked on the button before or not using Javascript in sharepoint page

Gulnar 146 Reputation points
2020-09-16T11:32:01.873+00:00

I want to edit in my code It's html,ccs, javascript code in sharepoint pge. I have a div with class "div" that contains a button that creates a new item when the user clicks on it using javascript function that contains create list item function and I have a table in my code with class"table".

My Javascript code is here for creating new list item:https://ibb.co/m8Xg6hp

I want to make if statement for if the current user was clicked the button before, then no need to show him the div that gives him the button to click again. so, It should show the table in this case.

However when the user doesn't clicked before on the button, it should show the div and after clicking on the button it should hide the div and show the table.

for more clarification please click on this image to see: https://ibb.co/pXp8zzv

How can I reach to this?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,796 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,571 Reputation points
    2020-09-17T02:10:44.997+00:00

    Hi @Gulnar ,

    Here is a code sample for your reference, checking if Item existed in another FlagList:

    25512-snipaste-2020-09-17-16-03-14.png

    FlagList field struct like this:

    25489-snipaste-2020-09-17-16-06-02.png

    The code snippet in the attachment, please check:

    25369-createitem.txt


    If the response is helpful, please click "Accept Answer" and upvote it.


2 additional answers

Sort by: Most helpful
  1. Gulnar 146 Reputation points
    2020-09-16T15:40:41.223+00:00

    No answer on my question :(

    0 comments No comments

  2. sadomovalex 3,631 Reputation points
    2020-09-16T15:54:25.027+00:00

    hello,
    as quick solution you may store flag to localStorage when user clicks the button:

    localStorage.setItem("myFlag", "1");
    

    and on page load hide button is flag is already there:

    if (localStorage.getItem("myFlag") === "1") {
        $("#myButton").hide();
    }
    

    But if user will cleanup local storage - button will be shown again. As an alternative you may also store item to cookies via javascript - but it also has own drawbacks (if user will open different browser or if cookies will be removed - button again will be shown).

    Robust solution may be implemented if you will store flag to some persistent storage (e.g. database or Sharepoint list). Check e.g. the following article: Add List Item in SharePoint List Using JavaScript. I.e. instead of localStorage you need to add flag to Sharepoint list and user login name of the current user as a key. In this case solution will work regardless of from which browser user opens the site.

    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.