Each function triggers only once
I'm trying to triggers a function on several owl-carousel sliders having the same class. If i do a console.log, it logs twice, but works only on the first slider.
Here's my function:
function setActiveItem()
var item = $('.owl-item');
var itemLink = $('.owl-item a');
var pos = $('.is-active').parent().parent().index();
$('.slider-mobile').each(function()
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
)
setActiveItem();
And here's a simplified version of my markup:
<ul class="list-container slider-mobile" data-stage-padding-items="200">
<li class="list-item">
<a href="#" class="title title--small list-link is-active">item</a>
</li>
</ul>
<ul class="slider-mobile" data-stage-padding-items="200">
<li class="list-item">
<a href="#" class="title title--small list-link is-active">item</a>
</li>
</ul>
I'm not sure if i'm using the each() function right, or if it fits my need at all.
javascript jquery each owl-carousel
add a comment |
I'm trying to triggers a function on several owl-carousel sliders having the same class. If i do a console.log, it logs twice, but works only on the first slider.
Here's my function:
function setActiveItem()
var item = $('.owl-item');
var itemLink = $('.owl-item a');
var pos = $('.is-active').parent().parent().index();
$('.slider-mobile').each(function()
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
)
setActiveItem();
And here's a simplified version of my markup:
<ul class="list-container slider-mobile" data-stage-padding-items="200">
<li class="list-item">
<a href="#" class="title title--small list-link is-active">item</a>
</li>
</ul>
<ul class="slider-mobile" data-stage-padding-items="200">
<li class="list-item">
<a href="#" class="title title--small list-link is-active">item</a>
</li>
</ul>
I'm not sure if i'm using the each() function right, or if it fits my need at all.
javascript jquery each owl-carousel
your simplified version of the HTML does not contain all the elements that are referenced in the javascript. Hard to tell what you are trying to achieve here then...
– Reinder Wit
Nov 12 at 12:44
Have you tried putting the selector.owl-item a
inside the find() method instead of a reference toitemLink
? My guess is that you are always referencing the same item so find will only be true in certain situations
– Reinder Wit
Nov 12 at 12:45
Just tried, doesn't work but thanks.
– Zac
Nov 12 at 12:47
add a comment |
I'm trying to triggers a function on several owl-carousel sliders having the same class. If i do a console.log, it logs twice, but works only on the first slider.
Here's my function:
function setActiveItem()
var item = $('.owl-item');
var itemLink = $('.owl-item a');
var pos = $('.is-active').parent().parent().index();
$('.slider-mobile').each(function()
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
)
setActiveItem();
And here's a simplified version of my markup:
<ul class="list-container slider-mobile" data-stage-padding-items="200">
<li class="list-item">
<a href="#" class="title title--small list-link is-active">item</a>
</li>
</ul>
<ul class="slider-mobile" data-stage-padding-items="200">
<li class="list-item">
<a href="#" class="title title--small list-link is-active">item</a>
</li>
</ul>
I'm not sure if i'm using the each() function right, or if it fits my need at all.
javascript jquery each owl-carousel
I'm trying to triggers a function on several owl-carousel sliders having the same class. If i do a console.log, it logs twice, but works only on the first slider.
Here's my function:
function setActiveItem()
var item = $('.owl-item');
var itemLink = $('.owl-item a');
var pos = $('.is-active').parent().parent().index();
$('.slider-mobile').each(function()
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
)
setActiveItem();
And here's a simplified version of my markup:
<ul class="list-container slider-mobile" data-stage-padding-items="200">
<li class="list-item">
<a href="#" class="title title--small list-link is-active">item</a>
</li>
</ul>
<ul class="slider-mobile" data-stage-padding-items="200">
<li class="list-item">
<a href="#" class="title title--small list-link is-active">item</a>
</li>
</ul>
I'm not sure if i'm using the each() function right, or if it fits my need at all.
javascript jquery each owl-carousel
javascript jquery each owl-carousel
edited Nov 12 at 12:48
Vadim Kotov
4,30153247
4,30153247
asked Nov 12 at 12:39
Zac
3718
3718
your simplified version of the HTML does not contain all the elements that are referenced in the javascript. Hard to tell what you are trying to achieve here then...
– Reinder Wit
Nov 12 at 12:44
Have you tried putting the selector.owl-item a
inside the find() method instead of a reference toitemLink
? My guess is that you are always referencing the same item so find will only be true in certain situations
– Reinder Wit
Nov 12 at 12:45
Just tried, doesn't work but thanks.
– Zac
Nov 12 at 12:47
add a comment |
your simplified version of the HTML does not contain all the elements that are referenced in the javascript. Hard to tell what you are trying to achieve here then...
– Reinder Wit
Nov 12 at 12:44
Have you tried putting the selector.owl-item a
inside the find() method instead of a reference toitemLink
? My guess is that you are always referencing the same item so find will only be true in certain situations
– Reinder Wit
Nov 12 at 12:45
Just tried, doesn't work but thanks.
– Zac
Nov 12 at 12:47
your simplified version of the HTML does not contain all the elements that are referenced in the javascript. Hard to tell what you are trying to achieve here then...
– Reinder Wit
Nov 12 at 12:44
your simplified version of the HTML does not contain all the elements that are referenced in the javascript. Hard to tell what you are trying to achieve here then...
– Reinder Wit
Nov 12 at 12:44
Have you tried putting the selector
.owl-item a
inside the find() method instead of a reference to itemLink
? My guess is that you are always referencing the same item so find will only be true in certain situations– Reinder Wit
Nov 12 at 12:45
Have you tried putting the selector
.owl-item a
inside the find() method instead of a reference to itemLink
? My guess is that you are always referencing the same item so find will only be true in certain situations– Reinder Wit
Nov 12 at 12:45
Just tried, doesn't work but thanks.
– Zac
Nov 12 at 12:47
Just tried, doesn't work but thanks.
– Zac
Nov 12 at 12:47
add a comment |
2 Answers
2
active
oldest
votes
Ok i figured it out.
@Reinder Wit you were right i was referencing the item of the first slider only, so both of my sliders were getting the same datas.
I did this:
function setActiveItem()
$('.slider-mobile').each(function()
var item = $(this).find('.owl-item');
var itemLink = $(this).find('.owl-item a');
var pos = $(this).find('.is-active').parent().parent().index();
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
console.log(pos);
)
setActiveItem();
And it is working now. Thanks for the clue.
great, good to hear
– Reinder Wit
Nov 12 at 14:35
add a comment |
You should activate your listener in the document ready like this:
$(document).ready(function()
setActiveItem();
);
It is i just didn't typed it here :)
– Zac
Nov 12 at 12:45
I think you need to change thisvar itemLink = $('.owl-item a');
tovar itemLink = $('.list-item a');
– McBern
Nov 12 at 12:51
Nope doesn't work. The function works as it is, but not on every instance of 'slider-mobile'
– Zac
Nov 12 at 12:58
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%2f53262389%2feach-function-triggers-only-once%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
Ok i figured it out.
@Reinder Wit you were right i was referencing the item of the first slider only, so both of my sliders were getting the same datas.
I did this:
function setActiveItem()
$('.slider-mobile').each(function()
var item = $(this).find('.owl-item');
var itemLink = $(this).find('.owl-item a');
var pos = $(this).find('.is-active').parent().parent().index();
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
console.log(pos);
)
setActiveItem();
And it is working now. Thanks for the clue.
great, good to hear
– Reinder Wit
Nov 12 at 14:35
add a comment |
Ok i figured it out.
@Reinder Wit you were right i was referencing the item of the first slider only, so both of my sliders were getting the same datas.
I did this:
function setActiveItem()
$('.slider-mobile').each(function()
var item = $(this).find('.owl-item');
var itemLink = $(this).find('.owl-item a');
var pos = $(this).find('.is-active').parent().parent().index();
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
console.log(pos);
)
setActiveItem();
And it is working now. Thanks for the clue.
great, good to hear
– Reinder Wit
Nov 12 at 14:35
add a comment |
Ok i figured it out.
@Reinder Wit you were right i was referencing the item of the first slider only, so both of my sliders were getting the same datas.
I did this:
function setActiveItem()
$('.slider-mobile').each(function()
var item = $(this).find('.owl-item');
var itemLink = $(this).find('.owl-item a');
var pos = $(this).find('.is-active').parent().parent().index();
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
console.log(pos);
)
setActiveItem();
And it is working now. Thanks for the clue.
Ok i figured it out.
@Reinder Wit you were right i was referencing the item of the first slider only, so both of my sliders were getting the same datas.
I did this:
function setActiveItem()
$('.slider-mobile').each(function()
var item = $(this).find('.owl-item');
var itemLink = $(this).find('.owl-item a');
var pos = $(this).find('.is-active').parent().parent().index();
if($(this).find(itemLink).is('.is-active'))
item.removeClass('active');
$(this).trigger('to.owl.carousel', [pos, 1, true]);
console.log(pos);
)
setActiveItem();
And it is working now. Thanks for the clue.
answered Nov 12 at 13:35
Zac
3718
3718
great, good to hear
– Reinder Wit
Nov 12 at 14:35
add a comment |
great, good to hear
– Reinder Wit
Nov 12 at 14:35
great, good to hear
– Reinder Wit
Nov 12 at 14:35
great, good to hear
– Reinder Wit
Nov 12 at 14:35
add a comment |
You should activate your listener in the document ready like this:
$(document).ready(function()
setActiveItem();
);
It is i just didn't typed it here :)
– Zac
Nov 12 at 12:45
I think you need to change thisvar itemLink = $('.owl-item a');
tovar itemLink = $('.list-item a');
– McBern
Nov 12 at 12:51
Nope doesn't work. The function works as it is, but not on every instance of 'slider-mobile'
– Zac
Nov 12 at 12:58
add a comment |
You should activate your listener in the document ready like this:
$(document).ready(function()
setActiveItem();
);
It is i just didn't typed it here :)
– Zac
Nov 12 at 12:45
I think you need to change thisvar itemLink = $('.owl-item a');
tovar itemLink = $('.list-item a');
– McBern
Nov 12 at 12:51
Nope doesn't work. The function works as it is, but not on every instance of 'slider-mobile'
– Zac
Nov 12 at 12:58
add a comment |
You should activate your listener in the document ready like this:
$(document).ready(function()
setActiveItem();
);
You should activate your listener in the document ready like this:
$(document).ready(function()
setActiveItem();
);
answered Nov 12 at 12:43
McBern
27026
27026
It is i just didn't typed it here :)
– Zac
Nov 12 at 12:45
I think you need to change thisvar itemLink = $('.owl-item a');
tovar itemLink = $('.list-item a');
– McBern
Nov 12 at 12:51
Nope doesn't work. The function works as it is, but not on every instance of 'slider-mobile'
– Zac
Nov 12 at 12:58
add a comment |
It is i just didn't typed it here :)
– Zac
Nov 12 at 12:45
I think you need to change thisvar itemLink = $('.owl-item a');
tovar itemLink = $('.list-item a');
– McBern
Nov 12 at 12:51
Nope doesn't work. The function works as it is, but not on every instance of 'slider-mobile'
– Zac
Nov 12 at 12:58
It is i just didn't typed it here :)
– Zac
Nov 12 at 12:45
It is i just didn't typed it here :)
– Zac
Nov 12 at 12:45
I think you need to change this
var itemLink = $('.owl-item a');
to var itemLink = $('.list-item a');
– McBern
Nov 12 at 12:51
I think you need to change this
var itemLink = $('.owl-item a');
to var itemLink = $('.list-item a');
– McBern
Nov 12 at 12:51
Nope doesn't work. The function works as it is, but not on every instance of 'slider-mobile'
– Zac
Nov 12 at 12:58
Nope doesn't work. The function works as it is, but not on every instance of 'slider-mobile'
– Zac
Nov 12 at 12:58
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%2f53262389%2feach-function-triggers-only-once%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
your simplified version of the HTML does not contain all the elements that are referenced in the javascript. Hard to tell what you are trying to achieve here then...
– Reinder Wit
Nov 12 at 12:44
Have you tried putting the selector
.owl-item a
inside the find() method instead of a reference toitemLink
? My guess is that you are always referencing the same item so find will only be true in certain situations– Reinder Wit
Nov 12 at 12:45
Just tried, doesn't work but thanks.
– Zac
Nov 12 at 12:47