Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I just recently got into coding and decided to just mess around before I started fully coding more in-depth. I am currently trying to make a simple game where you click an object, it does a little animation and it adds 1 to a counter.
I have the animation perfectly executed, however I am completely lost as to how I could make the counter. I am trying to use .js via the tags, mainly because the website never loads my script.js file, even though I reference it properly.
Anyways here is my html code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<h3>0 Clicks</h3>
<img id="choco" src="http://cdn.shopify.com/s/files/1/0210/5354/products/Chocolate_Chip_No_Background_1_medium.png?v=1365686508">
<img id="whirl" src="whirl.jpg" style="width:25em; height:25em;">
</body>
<script>
$(document).ready(function() {
('#cookie').click(function() {
function toClick(clicks){
return (clicks + 1);
print clicks;
};
});
});
</script>
</html>
My is very messy, I was trying a plethora of options to try and get it to work but it simply doesn't.
So basically what I am trying to do, is everytime I click the object I want it to add 1 to a counter I have on the page. 0 Clicks
I am not even sure that's possible but I hope someone out there can help me out :)
Thanks!
First, give your h3 an inner element that holds the click counter (and give that an ID so you can access it easily):
<h3><span id="clicks">0</span> clicks</h3>
Next - your click handler is missing the $ in front - but also has to get the current clicks, add 1, then re-print:
$('#cookie').click(function() {
var currentClicks = parseInt($("#clicks").text(), 10);
currentClicks++; //increment
$("#clicks").text(currentClicks); //set it
});
$(document).ready(function() {
$('#cookie').click(function() {
incrementCount();
});
});
function incrementCounter() {
var previousCounter = parseInt($("#counter").text());
previousCounter = isNaN(previousCounter) ? 0: ++previousCounter;
$("#counter").text(previousCounter);
}
function resetCounter() {
$("#counter").text(0);
}
Change HTML to
<body>
<h3><span id="counter">0</span> Clicks</h3>
The above code will look after even if you don't specify 0 with the Span.
I have just given you some bonus function - resetCounter too :)
Seems You are missing the $ sing in:
$('#cookie').click(function() {
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm working on my personal website and have implemented a preloader.
After adding a second div to my website, I noticed I can scroll during the page loading. I dunno about you but I find that ugly and disturbing.
Here is a quick video. (I use chromeOS)
Video
I really couldn't find anything on this because I think I was the only one with this problem. I'm not sure, however.
I used $(window).on("load",function(){$(".loader-wrapper").fadeOut("slow");}); as well
Here is the code (Github Repo)
Anyways, that's all I got.
Thanks in advance.
You can add by default a class to your body the class should be as follows.
// CSS
.no-scroll {
overflow: hidden;
}
<body class="no-scroll">
Once your script has completed or your function finishes you just call
document.body.classList.remove('no-scroll');
Make sure to add the following section at the end of your page.
<script type="text/javascript">
(function(){
function onReady() {
document.body.classList.remove('no-scroll');
}
if ( document.readyState === 'complete' ) {
onReady();
} else {
document.addEventListener('DOMContentLoaded', onReady);
}
})();
</script>
Or you can do it with jQuery
// Make sure this code is the last piece of code in your HTML.
$(window).on("load", function() {
document.body.classList.remove('no-scroll');
});
PS: Additionally to that consider the unlike scenario when someone does not have JavScript enabled so you add a default behavior. Take a look at <noscript> tag.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am new to javascript and I am working on a simple function. I am trying to get a button to redirect to another webpage on click, and I am attempting to implement it through javascript/jquery. Although I get no errors in the console, the functions simply don't fire for some reason. I have also utilized an alert to test, and even the alert does not come up. Any help is appreciated. All files are correctly linked to the HTML document. The commented out code was my javascript attempt and that did not work either.
//document.getElementById("updateB").onclick = function() {updateButton()};
$("#updateB").click(function() {
updateButton()});
function updateButton() {
alert("Working");
window.location.href = "http://www.w3schools.com";
}
function deleteButton() {
window.location.href = "http://www.w3schools.com";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td><button class ="buttonUpdate" id="updateB">Update</button></td>
<td><button class="buttonDelete">Delete</button></td>
I'm loading the scripts like this:
<script src="jquery-3.4.1.min.js"></script>
<script src="index.js"></script>
I think you should run the index.js inside the document ready function, button event listener would be added when the button is actually available. Until the dom is ready to button will not be available for the binding.
try adding a dom ready function and add your binding and other logic inside it.
$( document ).ready(function() {
$("#updateB").click(function() {
updateButton()});
});
https://learn.jquery.com/using-jquery-core/document-ready/
Happy coding.
Whenever you want to work with the DOM you have to make sure the DOM is ready by using JQuery's Document Ready function.
$(function(){
//document.getElementById("updateB").onclick = function() {updateButton()};
$("#updateB").click(function() {
updateButton()
});
});
function updateButton() {
alert("Working");
window.location.href = "http://www.w3schools.com";
}
function deleteButton() {
window.location.href = "http://www.w3schools.com";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td><button class ="buttonUpdate" id="updateB">Update</button></td>
<td><button class="buttonDelete">Delete</button></td>
Try running your code once onload is emitted. You can learn about it here.
window.onload = () => {
console.log('your code should be run in this function');
}
Your code runs properly. Only pay attention that, you have a commented code in the beginning.
The button event is triggered, the only reason the page is not redirecting to the w3schools, is because the browser is blocking such action.
Please see the snippet working below:
function updateButton() {
alert("Working");
window.location.href = "https://www.w3schools.com";
}
document.getElementById("updateB").onclick = function() { updateButton();
};
<td>
<button class ="buttonUpdate" id="updateB">Update</button>
</td>
<td>
<button class="buttonDelete">Delete</button>
</td>
Thanks.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to run the function renews() with interval but want to run first time when the html is opened. so I wrote renews(); but it doesn't work. I could run other function of the code. but only this one doesn't.
Thank you for the help!
<html>
<head>
<script>
var i=0;
var u=0; //0:redy for the next FX
var newstext;
function renews() {
if(i%2==0){
newstext = "aaa";
} else {
newstext = "bbb";
}
document.getElementById("news").innerHTML = newstext;
i++;
}
//renews(); // this doesn't work
setInterval(function(){
renews();
},3000);
</script>
</head>
<body>
<span id ="news">test</span>
</body>
</html>
Move your script to the bottom of the body. When you execute your script, the DOM has not been build yet, and thus there is no element to operate on.
When you’re dealing with the DOM it’s important to execute your code only after the page is fully loaded. If you don’t, there’s a good chance the DOM won’t be
created by the time your code executes.
Now Let’s think about what just happened when your code run. you put your js code in the header tag of the page, so it begins executing before the browser even sees the rest of the page. That’s a big problem because that span element with an id of “news” doesn’t exist, yet.
So what happened exactly? The call to getElementById returns null instead of the element you want, causing an error, and the browser, being the good sport that it is, just keeps moving and renders the page anyway, but without the change to the DOM at the first time.
How do you fix this? Well, you could move your code to the bottom of the body, but there’s actually a more foolproof way to make sure this code runs at the right time; a way to tell the browser “run my code after you’ve fully loaded in the page and created the DOM.” Besides moving the code to the bottom of the body, there’s another—and, one might argue—cleaner way to do it with code.
Here’s how it works: first create a function that has the code you’d like
executed once the page is fully loaded. After you’ve done that, you take the
window object, and assign the function to its onload property. The window object will call any function you’ve assigned to its onload property, but only after the page is fully loaded. Check This out:-
<html>
<head>
<script>
var i=0;
var u=0; //0:redy for the next FX
var newstext;
function renews() {
if(i%2==0){
newstext = "aaa";
} else {
newstext = "bbb";
}
document.getElementById("news").innerHTML = newstext;
i++;
}
//renews(); // this doesn't work
window.onload = renews; //But this will work
setInterval(function(){
renews();
},3000);
</script>
</head>
<body>
<span id ="news">test</span>
</body>
Now even if you put your JavaScript code in a separated JS file and import it in the header tag instead of moving it to the end of the body your code will work just fine.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a jQuery function that basically toggles a div on click but it doesn't work I can't tell if I maybe imported the library incorrectly or what but I can't get it to function at all. Please tell me if I am missing anything or messed up my code somewhere.
My exact code looks like this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a.notifications-dropdown-trigger").click(function () {
$('#post-notifications-dropdown').toggle('slide', {
duration: 100,
direction: 'up'
});
});
});
</script>
The HTML markup:
<a class="notifications-dropdown-trigger">click me</a>
<div id="post-notifications-dropdown">
.... code
</div>
Everything is coded directly onto servers and is live the moment I save.
You can simply use .slideToggle()
Description: Display or hide the matched elements with a sliding motion.
Code
$(document).ready(function(){
$("a.notifications-dropdown-trigger").click(function (event) {
$('#post-notifications-dropdown').slideToggle(100);
event.preventDefault();
});
});
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't seem to get my click trigger to work.
I've tried the following:
//Handle the Main Menu Button Press
$("button").click( function() {
alert('yep');
});
And I never get the alert when I click any button on my site....
Where did I go wrong?
might be one of these:
- you forgot to include jQuery.js script in your header
- you didn't wait till DOM loaded
try:
$( function() {
$("button").click( function() {
alert('yep');
});
});
Your code works just fine. Here is a fiddle
You need to ensure you have jQuery loaded, you have waited for the DOM to load using
$(function() {
//your code here
});
And if you have multiple buttons, you will need to use CSS selectors to differentiate.
I would HIGHLY suggest you read this document, provided by jQuery
The HTML could be
<button type="button" name="btnOk" id="btnOk">ok</button>
The jQuery code could be
$("button[type='button']").click(function(){
alert("yeap");
});
or
$("button[name='btnOk']").click(function(){
alert("yeap");
});
If HTML is
<input type="button" name="btnOk" id="btnOk" value="Ok"/>
the code could be
$("input[type='button']").click(function(){
alert("yeap");
});
At first, be sure that :has the jquery code in
$(document).ready(function(){
//jquery code
});