When the gets called, it must have a unique name for the function to execute

I've encountered an unusual problem when attempting to invoke a method in a JavaScript file. This particular .js file contains the following two methods:

function setCookie(c_name, value, exdays)
{
  //set the expiry date as now + the specified number of days
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);

  //add the expiry date string to the cookie value
  var c_value = escape(value) + ((exdays==null) ? "" : "; 
  expires="+exdate.toUTCString()+"; path=/");

  //set the cookie
  document.cookie = c_name + "=" + c_value;
}

function setTheCookie(c_name, value, exdays)
{
  //set the expiry date as now + the specified number of days
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);

  //add the expiry date string to the cookie value
  var c_value = escape(value) + ((exdays==null) ? "" : "; 
  expires="+exdate.toUTCString()+"; path=/");

  //set the cookie
  document.cookie = c_name + "=" + c_value;
}

When I click a button and use

onclick="setTheCookie('cookie_bar_hide', 'yes', 365)"
, it works fine. However, when I try
onclick="setCookie('cookie_bar_hide', 'yes', 365)"
, the method is not executed.

Does anyone have any insights into why this might be occurring?

Answer №1

Another way to set a cookie

As seen in the developer tools

> setCookie
function setCookie(name, value)
{
    document.cookie = escape($.trim(name)) + '=' + escape(value) + ';path=/';
}

After examining all of the JavaScript files, setCookie can be found in both main.js and utilities.js.

Answer №2

It seems like the issue could be related to how you are invoking your functions within the onClick event. Try calling multiple methods in a single line like this:

  onClick="doSomething(); doSomethingElse();"

Avoid using multiple OnClick's for separate functions.

Please inform me if this resolves the issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to trigger a mouse click event in a ReactJS environment

I am working on a project where I need to simulate a click (on the first item in a collapsed list) automatically without requiring any user interaction. The list collapsing feature is implemented using Material UI. Any suggestions or ideas would be great ...

Implementing a transition effect to the drawimage function

I am currently working on implementing a transition effect for an image inside a canvas element. I have provided a snippet below to demonstrate my progress so far. Can anyone guide me on how to incorporate a transition animation for the image within the c ...

Seamless side-to-side navigation

Currently, I am working on a website that has horizontal overflow instead of the common vertical scroll. While everything seems to be functioning properly, I need to enhance the user experience by adjusting the amount scrolled to move larger sections of th ...

Remove the most recently played sound from an array of sound using Vue.js

I've been trying to figure out how to randomize the sounds that play when a button is clicked, but I also want to avoid repeating the last played sound. It just doesn't sound right if the same sound plays repeatedly in quick succession. I'm ...

Is JSX Babel being rejected for execution?

I have recently started learning React and encountered an error while trying to run this page: Refused to execute script from 'https://unpkg.com/browse/[email protected]/babel.min.js' because its MIME type ('text/html') is not exec ...

What is causing the behavior of this JavaScript code in the Execution Context?

I recently delved into the world of asynchronous programming in JavaScript and wanted to share some code for examination: const myPromise = () => Promise.resolve('Success!'); function firstFunction() { myPromise().then(res => console. ...

Encountering a TypeError while utilizing a custom hook for an Apollo mutation operation

I'm facing a tough time debugging an issue with my Apollo mutation in my NextJS application. The error I'm encountering is as follows: "TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))" It appears t ...

Looking to extract the content from a div element without including the content of any nested divs, using jQuery and CSS selectors

Here is a div block: <div class='contatiner'> <div class='inner-div'>this is inner content</div> this is outer content </div> Below is the jQuery code being used: $(".container *:not(.inner-div)").h ...

Occasionally, jQuery Ajax will return a status of "0" in certain scenarios

As part of a psycholinguistic norming study conducted on Amazon's Mechanical Turk, participants are shown images and asked to name them out loud. Their voices are recorded using HTML5's audio recording features, and the recordings are then upload ...

What is the method for creating text that appears when the mouse is hovered over it?

My friend requested me to create a website for his YouTube channel with 4 links to different pages: Home, About Me, Merchandise, and Contact. He wants the text on these links to change color when hovered over, similar to terraria.org. I'm more profici ...

Visualizing data from an array within a different Vue component: A step-by-step guide

In my Vue app, I have successfully sent an array from one component to another. However, I am now facing a challenge in visually displaying this data. The issue arises when trying to extract and display specific values, like the date itself. Despite being ...

Issue - The 'defaultValue' is failing to load the state value, and the 'value' is not being updated when changed

My current setup involves an input field in my MovieInput.tsx file: <input id="inputMovieTitle" type="text" onChange={ e => titleHandleChange(e) } value={ getTitle() }> </input> This is how the titleHandleChange function ...

Easily use Discord.JS 13 slash commands to generate an embed displaying a user's specific role within a server

Currently, I am in the process of creating a slash command that will reveal a user's specific roles when the command is triggered. run: async (client, interaction) => { try{ const { member, channelId, guildId, applicationId, comman ...

Developing mobile-friendly applications with AngularJS, incorporating ngTouch into directives

Utilizing ngTouch to eliminate the delay on mobile devices has been effective, however, I have encountered an issue where clicking on an image does not trigger the expected behavior. In my application, clicking on an image is supposed to activate a directi ...

Using jQuery to hide individual elements within a group of elements with the same class based on specific text contained within them

Here is a demonstration of the task at hand - the HTML code: <div id="section"> <div class="test-row"> <div class="row-copy-options"> <h4 class="text-capitalize"><b>Test Heading 1</b></h4> ...

What is the process for updating npm on a Windows operating system?

Seeking advice on upgrading my npm version, I attempted Option 3 for Windows as outlined in the npm documentation. However, upon installation, I received a message stating that npm.exe already exists in the nodejs folder. Despite trying to overwrite it u ...

Having trouble with Javascript fetch() not receiving the correct JSON data from the local server

My Django backend is serving JSON data, but I'm encountering some unexpected results. When using curl 127.0.0.1:8000/posts/, the response includes: [ { "title": "This is a title", "body": "Body :)", "pub_da ...

Issues arise when JQuery functions fail to execute

This unique program showcases a series of error messages that are designed to appear under specific conditions. These conditions are identified through PHP code, following which the essential JQuery script is echoed via PHP to display the messages. Initia ...

Having trouble with Javascript Deserializer for my JSON object in an ASP.Net API Application

I've been trying different solutions I found online, but unfortunately none have worked for me. After fetching the JSON object from the API, I attempt to deserialize it into my class object. However, this process isn't successful (resulting in a ...

Basic selection menu layout

I have put together a basic menu for my WordPress site without relying on classes or IDs: <div class="main-menu"> <nav> <ul> <li> <a href="#" class="menu-link">home</a> ...