Why is jQuery.css() not working for me?
I've been struggling with some code and quite seem to get it to work - apologies if this is a silly question, I'm still quite new to JavaScript/jQuery etc.
I'm trying to build a Weather App as part of the Free Code Camp course and have built using simpleWeather.js. I'm now trying to animate a little bit of the page to make it a bit more interactive. I have some code:
$('#drop').on('click', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(180deg)');
});
I believe this should on a click, rotate the icon 180 degrees but no matter what variations I try, I can't seem to get it to work. After spending hours yesterday playing with it I decided it's time to ask for help! (The idea is that once it works, I'll animate the elements below so they only show when the button is clicked).
My codepen is https://codepen.io/woftis/pen/VjppYM
Thanks in advance for any help anyone can give.
javascript jquery css
add a comment |
I've been struggling with some code and quite seem to get it to work - apologies if this is a silly question, I'm still quite new to JavaScript/jQuery etc.
I'm trying to build a Weather App as part of the Free Code Camp course and have built using simpleWeather.js. I'm now trying to animate a little bit of the page to make it a bit more interactive. I have some code:
$('#drop').on('click', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(180deg)');
});
I believe this should on a click, rotate the icon 180 degrees but no matter what variations I try, I can't seem to get it to work. After spending hours yesterday playing with it I decided it's time to ask for help! (The idea is that once it works, I'll animate the elements below so they only show when the button is clicked).
My codepen is https://codepen.io/woftis/pen/VjppYM
Thanks in advance for any help anyone can give.
javascript jquery css
add a comment |
I've been struggling with some code and quite seem to get it to work - apologies if this is a silly question, I'm still quite new to JavaScript/jQuery etc.
I'm trying to build a Weather App as part of the Free Code Camp course and have built using simpleWeather.js. I'm now trying to animate a little bit of the page to make it a bit more interactive. I have some code:
$('#drop').on('click', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(180deg)');
});
I believe this should on a click, rotate the icon 180 degrees but no matter what variations I try, I can't seem to get it to work. After spending hours yesterday playing with it I decided it's time to ask for help! (The idea is that once it works, I'll animate the elements below so they only show when the button is clicked).
My codepen is https://codepen.io/woftis/pen/VjppYM
Thanks in advance for any help anyone can give.
javascript jquery css
I've been struggling with some code and quite seem to get it to work - apologies if this is a silly question, I'm still quite new to JavaScript/jQuery etc.
I'm trying to build a Weather App as part of the Free Code Camp course and have built using simpleWeather.js. I'm now trying to animate a little bit of the page to make it a bit more interactive. I have some code:
$('#drop').on('click', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(180deg)');
});
I believe this should on a click, rotate the icon 180 degrees but no matter what variations I try, I can't seem to get it to work. After spending hours yesterday playing with it I decided it's time to ask for help! (The idea is that once it works, I'll animate the elements below so they only show when the button is clicked).
My codepen is https://codepen.io/woftis/pen/VjppYM
Thanks in advance for any help anyone can give.
javascript jquery css
javascript jquery css
asked Jun 29 '16 at 7:32
StevenWalker
19715
19715
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Your #drop
element has been created after DOM load
i.e. dynamically. Hence you need event delegation
here. So attach your click to document
and then refer your element
- #drop
.
$(document).on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
Updated PEN
To be more simpler, since #weather
element existed during DOM load
, insted of document
you can add it to #weather
element. Consider below example.
$('#weather').on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
and also I believe, it should have been 180deg
to be proper turn-around.
Updated PEN 2 with 180deg and #weather
1
Parent element could be$("#weather")
...+1
– Rayon
Jun 29 '16 at 7:40
1
Brilliant - thank you :) Will accept once the 10 minutes is up!
– StevenWalker
Jun 29 '16 at 7:40
1
@Rayon.. Was updating the same.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
@StevenWalker. .Anytime.. Happy coding.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
add a comment |
I was having an issue with jquery .css
function with code that's very similar to to OP.
Problem
The .css
jquery function would not change the css on my .dot-one
or .dot-two
classes when I clicked button with the #correct-email
id.
Here's the required code examples:
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '$white');
$('.dot-two').css('background-color', '$gray-darker');
...
});
.dot-one {
background-color: $gray-darker;
}
.dot-two {
background-color: $white;
}
Solution
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '#fff');
$('.dot-two').css('background-color', '#222');
...
});
Apparently, jquery doesn't like variables SASS variables being passed into the .css
function. I couldn't find that anywhere in the documentation. My assumption is that it'd be able to compute the new value based off of the SASS variable. But it seems all that stuff is pre-processed by sass and not made available to css.
According to the SASS documentation this is what it sounds like is happening.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38093120%2fwhy-is-jquery-css-not-working-for-me%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your #drop
element has been created after DOM load
i.e. dynamically. Hence you need event delegation
here. So attach your click to document
and then refer your element
- #drop
.
$(document).on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
Updated PEN
To be more simpler, since #weather
element existed during DOM load
, insted of document
you can add it to #weather
element. Consider below example.
$('#weather').on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
and also I believe, it should have been 180deg
to be proper turn-around.
Updated PEN 2 with 180deg and #weather
1
Parent element could be$("#weather")
...+1
– Rayon
Jun 29 '16 at 7:40
1
Brilliant - thank you :) Will accept once the 10 minutes is up!
– StevenWalker
Jun 29 '16 at 7:40
1
@Rayon.. Was updating the same.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
@StevenWalker. .Anytime.. Happy coding.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
add a comment |
Your #drop
element has been created after DOM load
i.e. dynamically. Hence you need event delegation
here. So attach your click to document
and then refer your element
- #drop
.
$(document).on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
Updated PEN
To be more simpler, since #weather
element existed during DOM load
, insted of document
you can add it to #weather
element. Consider below example.
$('#weather').on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
and also I believe, it should have been 180deg
to be proper turn-around.
Updated PEN 2 with 180deg and #weather
1
Parent element could be$("#weather")
...+1
– Rayon
Jun 29 '16 at 7:40
1
Brilliant - thank you :) Will accept once the 10 minutes is up!
– StevenWalker
Jun 29 '16 at 7:40
1
@Rayon.. Was updating the same.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
@StevenWalker. .Anytime.. Happy coding.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
add a comment |
Your #drop
element has been created after DOM load
i.e. dynamically. Hence you need event delegation
here. So attach your click to document
and then refer your element
- #drop
.
$(document).on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
Updated PEN
To be more simpler, since #weather
element existed during DOM load
, insted of document
you can add it to #weather
element. Consider below example.
$('#weather').on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
and also I believe, it should have been 180deg
to be proper turn-around.
Updated PEN 2 with 180deg and #weather
Your #drop
element has been created after DOM load
i.e. dynamically. Hence you need event delegation
here. So attach your click to document
and then refer your element
- #drop
.
$(document).on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
Updated PEN
To be more simpler, since #weather
element existed during DOM load
, insted of document
you can add it to #weather
element. Consider below example.
$('#weather').on('click','#drop', function() {
$('.fa-chevron-circle-down').css('transform', 'rotate(225deg)');
});
and also I believe, it should have been 180deg
to be proper turn-around.
Updated PEN 2 with 180deg and #weather
edited Jun 29 '16 at 7:41
answered Jun 29 '16 at 7:36
Guruprasad Rao
24k858123
24k858123
1
Parent element could be$("#weather")
...+1
– Rayon
Jun 29 '16 at 7:40
1
Brilliant - thank you :) Will accept once the 10 minutes is up!
– StevenWalker
Jun 29 '16 at 7:40
1
@Rayon.. Was updating the same.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
@StevenWalker. .Anytime.. Happy coding.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
add a comment |
1
Parent element could be$("#weather")
...+1
– Rayon
Jun 29 '16 at 7:40
1
Brilliant - thank you :) Will accept once the 10 minutes is up!
– StevenWalker
Jun 29 '16 at 7:40
1
@Rayon.. Was updating the same.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
@StevenWalker. .Anytime.. Happy coding.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
1
1
Parent element could be
$("#weather")
...+1– Rayon
Jun 29 '16 at 7:40
Parent element could be
$("#weather")
...+1– Rayon
Jun 29 '16 at 7:40
1
1
Brilliant - thank you :) Will accept once the 10 minutes is up!
– StevenWalker
Jun 29 '16 at 7:40
Brilliant - thank you :) Will accept once the 10 minutes is up!
– StevenWalker
Jun 29 '16 at 7:40
1
1
@Rayon.. Was updating the same.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
@Rayon.. Was updating the same.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
@StevenWalker. .Anytime.. Happy coding.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
@StevenWalker. .Anytime.. Happy coding.. :)
– Guruprasad Rao
Jun 29 '16 at 7:42
add a comment |
I was having an issue with jquery .css
function with code that's very similar to to OP.
Problem
The .css
jquery function would not change the css on my .dot-one
or .dot-two
classes when I clicked button with the #correct-email
id.
Here's the required code examples:
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '$white');
$('.dot-two').css('background-color', '$gray-darker');
...
});
.dot-one {
background-color: $gray-darker;
}
.dot-two {
background-color: $white;
}
Solution
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '#fff');
$('.dot-two').css('background-color', '#222');
...
});
Apparently, jquery doesn't like variables SASS variables being passed into the .css
function. I couldn't find that anywhere in the documentation. My assumption is that it'd be able to compute the new value based off of the SASS variable. But it seems all that stuff is pre-processed by sass and not made available to css.
According to the SASS documentation this is what it sounds like is happening.
add a comment |
I was having an issue with jquery .css
function with code that's very similar to to OP.
Problem
The .css
jquery function would not change the css on my .dot-one
or .dot-two
classes when I clicked button with the #correct-email
id.
Here's the required code examples:
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '$white');
$('.dot-two').css('background-color', '$gray-darker');
...
});
.dot-one {
background-color: $gray-darker;
}
.dot-two {
background-color: $white;
}
Solution
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '#fff');
$('.dot-two').css('background-color', '#222');
...
});
Apparently, jquery doesn't like variables SASS variables being passed into the .css
function. I couldn't find that anywhere in the documentation. My assumption is that it'd be able to compute the new value based off of the SASS variable. But it seems all that stuff is pre-processed by sass and not made available to css.
According to the SASS documentation this is what it sounds like is happening.
add a comment |
I was having an issue with jquery .css
function with code that's very similar to to OP.
Problem
The .css
jquery function would not change the css on my .dot-one
or .dot-two
classes when I clicked button with the #correct-email
id.
Here's the required code examples:
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '$white');
$('.dot-two').css('background-color', '$gray-darker');
...
});
.dot-one {
background-color: $gray-darker;
}
.dot-two {
background-color: $white;
}
Solution
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '#fff');
$('.dot-two').css('background-color', '#222');
...
});
Apparently, jquery doesn't like variables SASS variables being passed into the .css
function. I couldn't find that anywhere in the documentation. My assumption is that it'd be able to compute the new value based off of the SASS variable. But it seems all that stuff is pre-processed by sass and not made available to css.
According to the SASS documentation this is what it sounds like is happening.
I was having an issue with jquery .css
function with code that's very similar to to OP.
Problem
The .css
jquery function would not change the css on my .dot-one
or .dot-two
classes when I clicked button with the #correct-email
id.
Here's the required code examples:
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '$white');
$('.dot-two').css('background-color', '$gray-darker');
...
});
.dot-one {
background-color: $gray-darker;
}
.dot-two {
background-color: $white;
}
Solution
$(document).on('click', '#correct-email', function () {
$('.dot-one').css('background-color', '#fff');
$('.dot-two').css('background-color', '#222');
...
});
Apparently, jquery doesn't like variables SASS variables being passed into the .css
function. I couldn't find that anywhere in the documentation. My assumption is that it'd be able to compute the new value based off of the SASS variable. But it seems all that stuff is pre-processed by sass and not made available to css.
According to the SASS documentation this is what it sounds like is happening.
answered Nov 20 at 6:01
alucinare
340310
340310
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38093120%2fwhy-is-jquery-css-not-working-for-me%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown