Local variable (in a function) to global (Jquery)
I have four <button>
tags, I need that when I click on the button tag, jQuery add the class Active
.
This is the code I have:
$(document).ready(function ()
var btnActive;
$(".cont-btn button:nth-of-type(1)").click(function ()
btnActive = 1;
);
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function ()
for (i = 0; i <= 4; i++)
if (i === btnActive)
$(".cont-btn button:nth-of-type(" + btnActive + ")").addClass("active");
else
$(".cont-btn button:nth-of-type(" + i + ")").removeClass("active");
);
);
Sorry for my English
PD. I get this error in the console at HTMLDocument.<anonymous> (proyectos.html:97)
javascript jquery
add a comment |
I have four <button>
tags, I need that when I click on the button tag, jQuery add the class Active
.
This is the code I have:
$(document).ready(function ()
var btnActive;
$(".cont-btn button:nth-of-type(1)").click(function ()
btnActive = 1;
);
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function ()
for (i = 0; i <= 4; i++)
if (i === btnActive)
$(".cont-btn button:nth-of-type(" + btnActive + ")").addClass("active");
else
$(".cont-btn button:nth-of-type(" + i + ")").removeClass("active");
);
);
Sorry for my English
PD. I get this error in the console at HTMLDocument.<anonymous> (proyectos.html:97)
javascript jquery
2
I get this error in the console
That's the origin of the error, but not the actual error
– CertainPerformance
Nov 15 '18 at 2:44
When you create the second click eventbtnActive
is undefined making the selector invalid.
– Spencer Wieczorek
Nov 15 '18 at 2:46
This is the error line$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {
– Lolete Sape
Nov 15 '18 at 2:49
add a comment |
I have four <button>
tags, I need that when I click on the button tag, jQuery add the class Active
.
This is the code I have:
$(document).ready(function ()
var btnActive;
$(".cont-btn button:nth-of-type(1)").click(function ()
btnActive = 1;
);
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function ()
for (i = 0; i <= 4; i++)
if (i === btnActive)
$(".cont-btn button:nth-of-type(" + btnActive + ")").addClass("active");
else
$(".cont-btn button:nth-of-type(" + i + ")").removeClass("active");
);
);
Sorry for my English
PD. I get this error in the console at HTMLDocument.<anonymous> (proyectos.html:97)
javascript jquery
I have four <button>
tags, I need that when I click on the button tag, jQuery add the class Active
.
This is the code I have:
$(document).ready(function ()
var btnActive;
$(".cont-btn button:nth-of-type(1)").click(function ()
btnActive = 1;
);
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function ()
for (i = 0; i <= 4; i++)
if (i === btnActive)
$(".cont-btn button:nth-of-type(" + btnActive + ")").addClass("active");
else
$(".cont-btn button:nth-of-type(" + i + ")").removeClass("active");
);
);
Sorry for my English
PD. I get this error in the console at HTMLDocument.<anonymous> (proyectos.html:97)
javascript jquery
javascript jquery
edited Nov 15 '18 at 2:51
Lolete Sape
asked Nov 15 '18 at 2:43
Lolete SapeLolete Sape
32
32
2
I get this error in the console
That's the origin of the error, but not the actual error
– CertainPerformance
Nov 15 '18 at 2:44
When you create the second click eventbtnActive
is undefined making the selector invalid.
– Spencer Wieczorek
Nov 15 '18 at 2:46
This is the error line$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {
– Lolete Sape
Nov 15 '18 at 2:49
add a comment |
2
I get this error in the console
That's the origin of the error, but not the actual error
– CertainPerformance
Nov 15 '18 at 2:44
When you create the second click eventbtnActive
is undefined making the selector invalid.
– Spencer Wieczorek
Nov 15 '18 at 2:46
This is the error line$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {
– Lolete Sape
Nov 15 '18 at 2:49
2
2
I get this error in the console
That's the origin of the error, but not the actual error– CertainPerformance
Nov 15 '18 at 2:44
I get this error in the console
That's the origin of the error, but not the actual error– CertainPerformance
Nov 15 '18 at 2:44
When you create the second click event
btnActive
is undefined making the selector invalid.– Spencer Wieczorek
Nov 15 '18 at 2:46
When you create the second click event
btnActive
is undefined making the selector invalid.– Spencer Wieczorek
Nov 15 '18 at 2:46
This is the error line
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {
– Lolete Sape
Nov 15 '18 at 2:49
This is the error line
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {
– Lolete Sape
Nov 15 '18 at 2:49
add a comment |
3 Answers
3
active
oldest
votes
There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:
$(".cont-btn").click(function ()
$(".cont-btn").removeClass("active");
$(this).addClass("active");
);
Example
Thank you very very very much, im a retard :facepalm:
– Lolete Sape
Nov 15 '18 at 3:04
add a comment |
You are making it more complicated than it needs to be.
The following should do what you need assuming that <button>
is inside class="cont-btn"
var $buttons = $(".cont-btn button").click(function ()
$buttons.removeClass('active');
$(this).addClass('active');
);
add a comment |
$(".cont-btn button:nth-of-type(" + btnActive + ")")
this code is executed only once.
Your code may be like this
$('.cont-btn button:eq(0)').addClass('active');
$('.cont-btn button').click(function()
$('.cont-btn button').removeClass('active');
$(this).addClass('active');
)
P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.
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%2f53311678%2flocal-variable-in-a-function-to-global-jquery%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:
$(".cont-btn").click(function ()
$(".cont-btn").removeClass("active");
$(this).addClass("active");
);
Example
Thank you very very very much, im a retard :facepalm:
– Lolete Sape
Nov 15 '18 at 3:04
add a comment |
There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:
$(".cont-btn").click(function ()
$(".cont-btn").removeClass("active");
$(this).addClass("active");
);
Example
Thank you very very very much, im a retard :facepalm:
– Lolete Sape
Nov 15 '18 at 3:04
add a comment |
There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:
$(".cont-btn").click(function ()
$(".cont-btn").removeClass("active");
$(this).addClass("active");
);
Example
There is a much simpler way to handle this. When you click the button remove the buttons of interest then add it to the element you clicked:
$(".cont-btn").click(function ()
$(".cont-btn").removeClass("active");
$(this).addClass("active");
);
Example
answered Nov 15 '18 at 2:53
Spencer WieczorekSpencer Wieczorek
17.6k43345
17.6k43345
Thank you very very very much, im a retard :facepalm:
– Lolete Sape
Nov 15 '18 at 3:04
add a comment |
Thank you very very very much, im a retard :facepalm:
– Lolete Sape
Nov 15 '18 at 3:04
Thank you very very very much, im a retard :facepalm:
– Lolete Sape
Nov 15 '18 at 3:04
Thank you very very very much, im a retard :facepalm:
– Lolete Sape
Nov 15 '18 at 3:04
add a comment |
You are making it more complicated than it needs to be.
The following should do what you need assuming that <button>
is inside class="cont-btn"
var $buttons = $(".cont-btn button").click(function ()
$buttons.removeClass('active');
$(this).addClass('active');
);
add a comment |
You are making it more complicated than it needs to be.
The following should do what you need assuming that <button>
is inside class="cont-btn"
var $buttons = $(".cont-btn button").click(function ()
$buttons.removeClass('active');
$(this).addClass('active');
);
add a comment |
You are making it more complicated than it needs to be.
The following should do what you need assuming that <button>
is inside class="cont-btn"
var $buttons = $(".cont-btn button").click(function ()
$buttons.removeClass('active');
$(this).addClass('active');
);
You are making it more complicated than it needs to be.
The following should do what you need assuming that <button>
is inside class="cont-btn"
var $buttons = $(".cont-btn button").click(function ()
$buttons.removeClass('active');
$(this).addClass('active');
);
answered Nov 15 '18 at 2:52
charlietflcharlietfl
140k1390124
140k1390124
add a comment |
add a comment |
$(".cont-btn button:nth-of-type(" + btnActive + ")")
this code is executed only once.
Your code may be like this
$('.cont-btn button:eq(0)').addClass('active');
$('.cont-btn button').click(function()
$('.cont-btn button').removeClass('active');
$(this).addClass('active');
)
P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.
add a comment |
$(".cont-btn button:nth-of-type(" + btnActive + ")")
this code is executed only once.
Your code may be like this
$('.cont-btn button:eq(0)').addClass('active');
$('.cont-btn button').click(function()
$('.cont-btn button').removeClass('active');
$(this).addClass('active');
)
P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.
add a comment |
$(".cont-btn button:nth-of-type(" + btnActive + ")")
this code is executed only once.
Your code may be like this
$('.cont-btn button:eq(0)').addClass('active');
$('.cont-btn button').click(function()
$('.cont-btn button').removeClass('active');
$(this).addClass('active');
)
P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.
$(".cont-btn button:nth-of-type(" + btnActive + ")")
this code is executed only once.
Your code may be like this
$('.cont-btn button:eq(0)').addClass('active');
$('.cont-btn button').click(function()
$('.cont-btn button').removeClass('active');
$(this).addClass('active');
)
P.s. I guess that you misunderstood how event handlers work. I would recommend you to read more on this topic. Execution flow in JS isn't easy to grasp.
answered Nov 15 '18 at 2:54
wanjaswanjas
30918
30918
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.
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%2f53311678%2flocal-variable-in-a-function-to-global-jquery%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
2
I get this error in the console
That's the origin of the error, but not the actual error– CertainPerformance
Nov 15 '18 at 2:44
When you create the second click event
btnActive
is undefined making the selector invalid.– Spencer Wieczorek
Nov 15 '18 at 2:46
This is the error line
$(".cont-btn button:nth-of-type(" + btnActive + ")").click(function () {
– Lolete Sape
Nov 15 '18 at 2:49