Error: The function is undefined and cannot be referenced

I have an "a href" tag that calls a function when clicked, but it's not working and I'm getting this error:

Uncaught ReferenceError: Gonder is not defined index.php:10
onclick 

Here is the JavaScript code :

<script type="text/javascript">  
$(document).ready(function(){
    function Gonder(nereden, nereye) {

       $.ajax({
            type: "POST",
            url: "/ara.php",
            data: '{ "nereden":"' + nereden + '", "nereye":"' + nereye + '" }',
            contentType: 'application/json; charset=utf-8',
            success: function (result) {              
                $("#sonuc").html(result.d);

            },
            error: function (result) {

                $("#sonuc").html(result.d);


            }
        });
    }

});

</script>  

And here is the HTML code :

<a href="javascript:void(0)" onclick="Gonder(100, 101);" title="">
click</a>

This setup is not functioning as expected...

Answer №1

Errors may occur when a function is not properly defined or contains bugs.

In this particular scenario, the issue lies in the following code:

data: '{ "nereden":"' + nereden + '", "nereye":"' + nereye + '" }',

The correct version should be:

data: { nereden: nereden, nereye: nereye },

Answer №2

Just wanted to add my thoughts,

I believe moving the function definition outside of the document ready block will resolve the issue and allow you to utilize the function. So, try defining the function outside of document ready like this, and consider changing the data format to Piwolli's format instead of your current one if the data is not being sent correctly to the PHP code.

function SendData(from, to) {

   $.ajax({
        type: "POST",
        url: "/search.php",
        data: '{ "from":"' + from + '", "to":"' + to + '" }',
        contentType: 'application/json; charset=utf-8',
        success: function (response) {              
            $("#result").html(response.d);

        },
        error: function (response) {

            $("#result").html(response.d);

        }
    });
}

Answer №3

Give this a shot:

http://jsfiddle.net/o24fm/

XHTML

<div id="sample" class="example">hello world</div>

JavaScript

$(document).ready(function(){

$("#sample").on("mouseover", function(){
    $(this).toggleClass("highlight");
});

});

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

Converting an anonymous function to a named function in JavaScript

In my Angular application, I have the following template: <dxi-column dataField="ordre" caption="Ordre" [width]="70" dataType="number" [allowEditing]="true"> <dxi-validation-rule type=" ...

Is it common for Google Chrome console snapshots to have a prolonged idle time?

I noticed in Google Chrome console snapshot that my JavaScript code has a long "Idle" time. I'm not sure if this is normal. Attached is a screenshot for reference: https://i.stack.imgur.com/k0mkp.png Could this extended "Idle" time be attributed to ...

Tips for fixing the issue "Uncaught SyntaxError: Unexpected token S in JSON at position 0"

After referencing sols of SO, I am still unable to solve the error. I have a file dashboard.html which contains search conditions. When clicked, it calls loadtable.js. This loadtable.js file uses search.php to retrieve rows from a table. However, encount ...

Module compilation error: BrowserslistError: Invalid browser query 'dead' was not recognized

Whenever I attempt to execute "npm run build" in the command prompt, I encounter an error message stating: "Module build failed: BrowserslistError: Unknown browser query dead at Array.forEach()". I have experimented with the commonly suggested fix found o ...

The safe area of the iPhone X webpage

Discovering the perfect iPhone X landscape safe area and notch adjustment. I decided to share this inquiry in order to assist those struggling with adjusting their websites for the latest iPhone model 10. The main issue revolves around pesky white bars a ...

The placement of the button is not correct and should be adjusted to the proper position

I have created a webpage with two distinct sections, each occupying the height of the viewport. The first section contains a 'work' button in the center. Upon clicking this button, it disappears and is replaced by some links. The same functionali ...

What could be causing bundle.js to remain in a pending status on my website?

Whenever I try to open a page from my project, the browser keeps showing loading forever and in the network tab, the status of bundle.js is pending. (Interestingly, if I open other routes of the project, everything works fine). If I remove all product var ...

Similar to form requests in AJAX

Form submissions not only send data to the server, but they also direct the user to a specific resource using a specified method. I am looking to replicate this functionality using AJAX. For instance, I would like to achieve the same result as the form re ...

Challenges with dynamically creating buttons and using a global array variable in Jquery

I have a global array variable that I need to access from multiple functions. The problem arises when I try to use this variable in a function that is bound to a button created dynamically, as the variable is undefined at that point. var arr = [[]]; ...

MongoDB failing to enforce unique constraints on partial indexes

I have a set of data that looks like this: { name: "Acme co." add4: "", nationalNumber: "+13412768376" }, { name: "Acme Inc.", add4: "6345", nationalNumber: "" } My goal is to insert these records into a collection, but only if they are uni ...

Communicate data from a slot to a Vue.js component instance

I am looking to only pass a specific portion of the data to an instance component. For example, I have a table component with a cell slot (e.g. name), and I would like to send the cell value to the instance while also having the ability to customize or o ...

Is it possible to adjust the size of my image based on the browser's zoom level?

While working on my website, I noticed that when I zoom out on my browser, everything resizes except for the image. The image remains the same size regardless of whether the browser is set at 100% or 50%. Is there a way to code the image so that it resizes ...

The resume button is failing to activate any functions

I recently encountered an issue with a JS file that is associated with a Wordpress Plugin, specifically a Quiz plugin featuring a timer. I successfully added a Pause and resume button to the quiz, which effectively pauses and resumes the timer. However, I ...

Angular and RxJS work together to repeat actions even when an HTTP request is only partially successful

I am currently attempting to achieve the following: Initiate a stop request using a stored ID in the URL. Send a request with a new ID in the URL and save this new ID. If the response code is 200, proceed as normal. If it is 204, repeat steps 1 and 2 with ...

Are AJAX objects supported in both Safari and Chrome?

I asked, Could you please assist me in understanding why my searches work perfectly in IE8 but encounter issues with Safari and Chrome? The website I am referring to is www.netivot.biz. To handle ajax requests, I have employed the following code at www. ...

What could be the reason for my Javascript and JQuery code not functioning properly?

Recently, while browsing through a question thread I stumbled upon some advice given to an individual whose Javascript code was not functioning properly. They were suggested to place a function that started on loading of the page. I decided to experiment ...

Using jQuery to attach a data attribute (including special characters) to a DOM element

I'm having issues adding a data attribute to an HTML element. The data attribute (data-description) may include special characters like apostrophes. InsertHtml = InsertHtml + '<tr id="DashboardRow0" data-description=\'' + JSON. ...

AJAX function in Chrome console is throwing an error message stating "Unexpected Token }"

Dealing with this issue has been quite unusual for me. I've spent the last 3 days trying to troubleshoot it, but now it's no longer bothering me. The situation involves a button and a textbox that sends the data from the textbox to a PHP page whe ...

Turn off Node.js Caching for Good

My Node.js website seems to be stuck using a cached version even after updating the code and using the -w option. I need to figure out how to disable caching with Forever in Node.js and completely uninstall it. I found this answer that confirms it caches: ...

Issues arose in retrieving an error message through AJAX, a problem limited to the phone platform

My web app has been running smoothly without any bugs for quite some time. However, every now and then, there seems to be an issue - possibly related to network connectivity - that I can't quite pinpoint. Oddly enough, this only happens on a phone (sp ...