Trying to change the class of an item with jQuery, but I can't seem to make it work. For some reason, nothing at all is happening.
Code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$("#theSpinner").click(function() {
$("#theSpinner").toggleClass("spinning");
});
</script>
<a href="#" id="reload">
<img src="spinbut.png" style="width:100px;height:100px;" id="theSpinner" class="notspinning">
</a>
I've tried with:
$("#theSpinner").click(function() {
$("#theSpinner").removeClass("notspinning");
$("#theSpinner").addClass("spinning");
});
aswell as $("#reload") instead of $("#theSpinner"), and with an onClick="" to a javascript function containing the addClass / removeClass part.
I really don't know what's the problem, but any way around this bull would make me very happy.
Try wrapping your jQuery code within a "document-ready" block to ensure that jQuery has been loaded prior to calling your function :
<script>
// Example of a document ready function
$(function(){
// When jQuery has loaded, wire up this function
$("#theSpinner").click(function() {
$(this).toggleClass("spinning");
});
});
</script>
Example
You can see a working example of this in action here and demonstrated below :
Here you go
<img src="spinbut.png" style="width:100px;height:100px;" id="theSpinner" class="notspinning">
<script type="text/javascript">
$("#theSpinner").click(function() {
$(this).toggleClass("notspinning spinning");
});
</script>
Related
i have little problems with JavaScript onClick function.
I need function that will get class, for example .modal. It's look like this right now
<div class="modal" onclick="modalFix()">
<p>here is some text</p>
</div>
And there is script that works, but i must delete onclick="modalFix()"
<script type="text/javascript">
function modalFix(){
$("body").addClass("modal-open");
};
</script>
How to rewrite that same JavaScript function so i can call div class from it?
You can use following script.
JavaScript:
$(document).ready(function() {
$('.model').click(function() {
$("body").addClass("modal-open");
});
});
I am using a bit of jquery to hide some content on my webpage until a button is clicked. The jquery is hiding my content just fine but no matter how many times I click the button, it wont show back up again and I cant figure out what I have done wrong! Here is my code:
$(document).ready(function() {
$("#hidden-samples").hide();
$('.access-content').click(function() {
$('#hidden-samples').show();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hidden-samples">Samples</div>
<a class="access-content">Show Samples</a>
Instead of show() use toggle() method. Please check the code below:
$(document).ready(function() {
$("#hidden-samples").hide();
$('.access-content').click(function() {
$('#hidden-samples').toggle();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hidden-samples">Samples</div>
<a class="access-content">Show Samples</a>
I have the following function:
$(document).ready(function(){
$('section').mouseenter(function(){
var id = $(this).attr('id');
$('a').removeClass('colorAdded');
$("[href=#"+id+"]").addClass('colorAdded');
});
});
For some reason, the mouseenter function does not seem to be working. When the mouse scrolls over one of the sections (which have the section tag), the corresponding link (which have the a tag) in the top right corner should gain a green background color. However, it is unresponsive.
You just need to change your JS to:
$(document).ready(function(){
$('section').mouseenter(function(){
var id = $(this).attr('id');
$('a').removeClass('colorAdded');
$("a[href='#"+id+"']").addClass('colorAdded');
});
});
It was an issue with not including quotations in selecting the a tag with the href targeting.
I'm actually don't know why your codepen example is not working, I didn't look into your code carefully, but I tried to create simple code like bellow and it worked.
the thing you probably should care about is how you import JQuery into your page.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
hopefully this following codes can help you.
$(document).ready(function(){
$('button').mouseenter( function() {
console.log('hahaha');
})
});
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="test">
<button class="test-button" > test button </button>
</div>
</body>
</html>
Returning to JavaScript after a long break and I can't get this to work. I must be doing thing really stupid because it works fine with an onclick('') attached to the button in the HTML file just not when I try and do it in the script.js
I've tried to include everything that should be needed but if i've missed something let me know.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"</script>
<script type="text/javascript" src="script.js"></script>
<div class="application">
<button type="button" class="solution" id="webSolution">
<p>
Website Solutions
</p>
</button>
<button type="button" class="solution" id="mobileSolution">
<p>
Mobile Solutions
</p>
</button>
</div>
<div class="mobileSolutionDetails">
<h3>
Mobile Solution
</h3>
price details
</div>
<div class="webSolutionDetails">
<h3>
Website Solution
</h3>
price details
</div>
and my javascript
$(document).ready(function() {
$('#webSolution').onclick(function() {
alert('webSolution');
$('.webSolutionDetails').style.display = 'block';
$('.mobileSolutionDetails').style.display = 'none';
});
$('#mobileSolution').onclick(function() {
alert('Hey!');
$('.mobileSolutionDetails').style.display = 'block';
$('.webSolutionDetails').style.display = 'none';
});
});
Any help would be great
It doesnt like onclick for one. Changed to:
.on("click", function(){});
instead of
.onclick(function(){})
if you wanted to do it your way, you would want to do
.click(function(){});
on a similar note, your CSS tags are wrong for jquery.
use:
$(item).css({display:"block"}); //instead
Here is a fiddle. http://jsfiddle.net/qMsm2/
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
Your script must come after jquery.min.js, otherwise $ won't be defined when you try to use it. You're also missing the end > in the jQuery script tag.
jquery uses "click" as function.
http://api.jquery.com/click/
You should use it like this:
$('#webSolution').click(function() {
});
Check this fiddle: http://jsfiddle.net/4CNML/
Two ways for you to write it.
With .on (this is becoming the most favored way it seems)
$('#mobileSolution').on('click', function() {
alert('Hey!');
$('.mobileSolutionDetails')..css({display: 'block'});
$('.webSolutionDetails').css({display: 'none'});
});
or with .click
$('#mobileSolution').click(function() {
alert('Hey!');
$('.mobileSolutionDetails').css({display: 'none'});
$('.webSolutionDetails').css({display: 'none'});
});
Also, since your using jQuery the .style is invalid. Use .css like I did above.
I am an absolute beginner in this field. I have written the following code but it's not working. Can someone explain where is the error?
<script type="text/javascript">
$("button").click(function(){
$("div").show("slow");
});
</script>
<button>show</button>
<div style="min-height:300px; min-width:400px; border:10px solid grey; display:none;"></div>
this is a link
You execute the jQuery before the button is there. So at the point jQuery is executed, the button is not existent.
The solution is to use $(document).ready(...).
All elements on a page (HTML) are parsed to the DOM of that page. The DOM is a hierarchical collection of all elements. You can select elements out of it using jQuery.
However, obviously the DOM needs to be fully there before you can select the elements. In your case, you first select the button, and create it afterwards. That does not work. At the time of the selector the button does not exist yet.
Using jQuery's $(document).ready(), you can defer the function containing selectors until the DOM is fully loaded and parsed - at which time the selector works because the elements are there.
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function(){
$("div").show("slow");
});
});
</script>
<button>show</button>
<div style= "min-height:300px; min-width:400px; border:10px solid grey; display:none;"></div>
this is a link
You may be trying to listen for the click event before the button exists. Try waiting for the page contents to load:
<script type="text/javascript">
$(document).ready(function() {
$("button").click(function(){
$("div").show("slow");
});
});
</script>
<button>show</button>
<div style= "min-height:300px; min-width:400px; border:10px solid grey; display:none;"></div>
this is a link
As you can see from the fiddle here http://jsfiddle.net/bQUFE/ your code works (the div is shown when you click)
If you don't have this result always check:
1)that you loaded jquery before your script
2)Always use $(document).ready() like this:
$(document).ready(function(){
$("button").click(function(){
$("div").show("slow");
});
});
the javascript is executed before the document has loaded, so the click-handler can't be attached to the button. to avoid this, wrap your code into a domready-function like this:
$(document).ready(function() {
$("button").click(function(){
$("div").show("slow");
});
}
It probably is working but as your <div></div> is empty you might notice it.
Also make sure you only run your JQuery script AFTER the div has been added to your HTML.
May I also suggest to use better identifiers, because you might eventually have many div's and buttons on your page.
Example:
<button id="btn1">Click me</button>
<div id="hidden_div" style="display: none">
Hello there!
</div>
<script type="text/javascript">
$('button#btn1').click(function() {
$('div#hidden_div').show('slow');
});
</script>
You code runs before the button is created so it cannot bind the event.
So you need to put your code in the document.ready event. In jQuery this is handled by $(document).ready(handler) or $(handler).
Documentation at http://api.jquery.com/ready/
Fix to your code to make it work
$(function(){
$("button").click(function(){
$("div").show("slow");
});
});
demo at http://jsfiddle.net/gaby/vkrzt/
you need to give your elements "id"
so:
<button id="mybutton"></button>
and
<div id="mydiv"></div>
Then you'll use $("#mybutton") or $("#mydiv") to call these