The JavaScript syntax dollar sign

I am currently studying a JavaScript source code and it's my first time writing JavaScript. I find some of the syntax confusing.

<script id="source" language="javascript" type="text/javascript">

$(function () {    

 window.onload=function()
 {
mkAjaxObj(0,0);
 }    
});

</script>

Can you explain why the anonymous function is enclosed in $( ), what is the purpose of $( )? Will the anonymous function be automatically called or executed?

Another question:

var plot = $.plot($("#placeholder"),
          [ { data: monkeyavgtime, label: "monkey average run time"}, { data: monkeymixtime, label: "monkey shortest run time" } ], 
          {
              series: {
              lines: { show: true },
              points: { show: true }
              },
              grid: { hoverable: true, clickable: true },
              yaxis: { min: 0, max: maxtime },
              xaxis: {ticks:[[0,version[0]],[1,version[1]],[2,version[2]], [3,version[3]], [4,version[4]],[5,version[5]],[6,version[6]]]}
          });

1 : In $.plot, what does the $ stand for?

2 : $("#placeholder") There is an HTML tag:
Is $("#placeholder") referencing it? Or is there any difference compared to using var obj=document.getElementById("product")?

Thank you very much!

Answer №1

$ is simply a placeholder for a variable. It doesn't hold any unique significance on its own. The reason it's frequently used in libraries like jQuery and Prototype is due to its brevity and ease of typing.

Answer №2

When working with JavaScript, it is important to note that variable names can actually start with a $, such as $foo = 42. Surprisingly, even just using a $ as a variable name is allowed.

Although the core JavaScript language does not utilize this specific naming convention, certain JavaScript libraries like Prototype and jQuery introduce a global variable called $ that serves as a function with additional features. This special function allows for convenient selection of DOM elements using CSS-like expressions.

Answer №3

It seems that the symbol "$" is likely referring to the jQuery function in this context. In JavaScript, "$" can be used as a valid variable name, so certain libraries choose to use it as a shorthand for another function, such as jQuery. Here, the anonymous function gets executed by jQuery once the DOM has finished loading. If you're unfamiliar with these concepts, I suggest diving into some beginner-level JavaScript resources to gain more insights.

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

Discovering the final step of a for loop when working with JavaScript objects

Currently, my data consists of: {12: Object, 13: Object, 14: Object} I need help figuring out how to detect the final iteration in the for loop below: for (var i in objs){ console.log(objs[i]); } Does anyone have any solutions? ...

SignalR 2.2 users are experiencing a lack of message reception

I have developed a unique self-hosted SignalR application that is operating within the framework of a console application. To access the hubs within this application, I have implemented a wrapper class to avoid referencing the SignalR.Core assemblies direc ...

Unable to view the HTML division

I am struggling with my website creation as I encounter issues with the .Main div. Despite setting background-color: orange; in the CSS, the color is not visible on the page. The inspector shows that it is positioned at 1440 x 0. Any assistance would be gr ...

Capture an image of the element

Hi there, I'm currently attempting to use PhantomJS to capture a screenshot of a specific element. I'm utilizing the PhantomJS bridge for Node.js: phantom Here's what I have so far: page.includeJs('http://ajax.googleapis.com/ajax/libs ...

Utilizing multiple class names in Material UI with identical definitions

When working with traditional CSS, it is common to define shared properties between classes like this: .classA,.classB{ background-color: black; } In Material UI and using theming, the equivalent would be: styles = (theme)=>({ classA:{ ba ...

Please be patient until setInterval() completes its task

In order to add a dice-rolling effect to my Javascript code, I am considering using the setInterval() method. To test this out, I have come up with the following code: function rollDice() { var i = Math.floor((Math.random() * 25) + 5); var j = i; ...

Modifying the value of a property in an object array created using the map method is ineffective

I have a collection of objects: https://i.sstatic.net/XNrcU.png Within the collection, I wished to include an additional property to the objects. To achieve this, I utilized the map function: returnArray = returnArray.map((obj) => { obj.active = "fal ...

Clicking on the (x) button element will eliminate the DOM node from a list

https://i.stack.imgur.com/GxVYF.png A value is being dynamically added to my page, and here is the code snippet: function favJobs(data){ number_of_jobs_applied = data.total_bookmarked; $('.bookmark-title').append(number_of_jobs_applied, " ...

Is there a way to showcase the uploaded file contents on the current page without the need to refresh?

Is there a way to display the contents of an uploaded file on the same HTML page without opening a new tab or refreshing it? I have the following HTML and PHP codes for reading an uploaded file in a separate page, but I want to integrate it into the same H ...

Unable to retrieve variable using the require statement

When attempting to access the variable 'app' from the required index.js in my test, I encounter difficulty resolving the 'app' variable. server.js 'use strict'; var supertestKoa = require('supertest-koa-agent'); ...

Executing NodeJS awaits in the incorrect order - When using Express with SQLite3's db.all and db.run, one await is prioritized over the other

Need help running asynchronous functions in .exports, getting promises, and using the results in subsequent async functions. Despite using awaits, the second function seems to execute before the first one. sales.js = const sqlite3 = require('sqlite ...

Exploring JSON data structures using autocomplete functionalities

Here's the code I'm working with: <s:hidden id="s" value="%{Users}"/> The variable Users contains an array list of User objects. This code is written in Javascript. I want to access Users as JSON for auto-complete functionality: var valu ...

Show a compact graphic in the upper-right-hand corner

Hey, I have this interesting idea but CSS isn't my strong suit. Any thoughts on how to achieve it? I'm looking to create a new class that, when applied to an item (like a div), displays a small clickable pre-defined image in the Top-Right corne ...

Unable to bypass YouTube advertisement

I am currently experimenting with using nodejs puppeteer to test if I can bypass the ad on Youtube. Although this is just for testing purposes, I am facing some challenges with getting it to work as expected. I have implemented a while loop to search for ...

Ways to focus on a specific div using JavaScript

I am attempting to create a 'snow' effect on the background of a particular div with a red border. Here is the code, but you can also view it here: <!-- language: lang-js --> var width = getWidth(); var height = getH ...

Transferring the state from a parent component to a child function

I'm having trouble passing a state from a component to a function. I was able to pass the state from Home to ListHome, but I'm struggling to continue passing it to a function within ListHome (Slider) because it's a function. Even after revi ...

The state object in Redux Toolkit remains static, resulting in the error message "Uncaught TypeError: Cannot assign to read only property 'property-name' of object '#<Object>'"

I'm facing an issue where I am trying to update the data of a state object in Redux Toolkit but it's not changing, and instead throwing an error that says Uncaught TypeError: Cannot assign to read only property 'status' of object ' ...

I am facing an issue with my if statement not functioning properly when verifying a radio input

Trying to validate radio input to confirm if it's the correct answer, but encountering an issue where it skips the line if (answer == allQuestions[q].correctAnswer). Here is the complete code: https://jsfiddle.net/alcatel/sopfmevh/1/ for (let k = 0; ...

Express.js Res redirection problem occurring with Backbone.js due to failure in redirecting hashtag URLs

Issue Summary: Having trouble with Express.js redirect functionality. The problem occurs when trying to redirect after entering /#impulse, but works fine with /impulse/. Server processes the request for /#impulse before applying redirect checks, which re ...

Using window.print as a direct jQuery callback is considered an illegal invocation

Curious about the behavior when using Chrome $(selector).click(window.print) results in an 'illegal invocation' error $(selector).click(function() { window.print(); }), on the other hand, works without any issues To see a demo, visit http://js ...