Explanation for the Code [closed] - javascript

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 asked a question a few days back, and the link below was the solution in JSFiddle:
http://jsfiddle.net/vineetgnair/pnhxxcsw/11/
Later on, I got another answer that did the work of the previous solution in just 2 lines of code instead of 8-9. Since I'm new to coding, I'm unable to figure out how the new JSFiddle works. If someone could explain it to me, I would be appreciative.
Here is the code and JSFiddle link from the second solution:
var div = $('div').not(':first').hide().end();
$('button').on('click', function() { div.hide().eq($(this).index()).show() })
http://jsfiddle.net/adeneo/pnhxxcsw/13/
Thanks in advance

the first line is very straight forward to read, we're getting all the divs, filtering them to get everything that is not the first child of it's parent, and hiding them, in other words, hiding everything except for first children.
the second line, we are adding a click event listener to buttons, and when we click, we show only the element with an index matching the clicked button
one more thing, just like the comments stated, check the jquery docs, that's the place to go in these cases

Related

How to use attr in elements within an array? [closed]

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 2 years ago.
Improve this question
So, I have an array with 200 items called "icone", and when I use this code:
icone.attr('src', 'img/known.svg');
All items change according to what is in the code.
But how do I do if I want to change only 1 of the items in this array, and not all 200?
I tried with:
icone[0].attr('src', 'img/known.svg');
But the console returned "icone[0].attr is not a function".
Any ideas on how to make this work?
Thanks!
If it is absolutely necessary to use jQuery in this case. You can do this:
$(icone[0]).attr('src', 'img/known.svg');
In case it is not, you can do it as the other answers say.
Once you do icone[0], you're dealing with a raw node, not a jQuery "wrapper." That raw element does not have an .attr function.
As Ouroborus suggests, just set the attribute directly by doing icone[0].src = ....
(If Ouroborus submits an answer in addition to their comment, you should mark it as accepted, not this answer. I just wanted to explain why what you're doing doesn't work.)
EDIT: Interestingly, icone.first().attr(...) does work, because it does wrap the first element with a jQuery wrapper.

JS Can not use click function after append [closed]

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 6 years ago.
Improve this question
I add my TRIGGER DIV A over $(".DIVB").html(data); from a AJAx Responsebut when i now want to trigger some code over the $(".TRIGGER DIV A").click(function() it isnt working. Can someone explain me why ? And is there a Way to fix this or is there a working arround ?
It looks like you're using jQuery's .click(). If HTML is added dynamically to the DOM, you need to bind the click event to the element after it has been added. Otherwise, when $(".TRIGGER DIV A").click(handler) runs and jQuery looks for the element to bind, it isn't able to find it.
You may consider using .delegate() instead. This ensures that the event is bound to all elements relevant to the given selector regardless of when it is added to the DOM. You can find the documentation for usage here: http://api.jquery.com/delegate/

Script turns page white [closed]

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 6 years ago.
Improve this question
so this is a very very simple script. however... every time i run it inside chrome, by the "onmouseover" my entire page turns white. i have no idea why?
this is the script. it was way larger. but i seems to fail at this point...
already tried with other measurements. always the same.
function open(){
document.getElementById("gtext").style.marginTop = "37vh";
document.getElementById("gpic").style.marginTop = "37vh";
document.getElementById("gvid").style.marginTop = "37vh";
document.getElementById("gaudio").style.marginTop = "37vh";
}
maybe someone here can help me.
thanks.
Here my Pen:
http://codepen.io/anon/pen/RarJVR
Try renaming your function to something else like e.g. openMenu
Sounds like you need to debug your code (are you asking the community to do it?). For starters, opacity can only take number values (not vh):
if(document.getElementById("gtext").style.opacity == "37vh") {

jQuery plugins not working on nested pages [closed]

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
so I am working on this site (my rep isn't good enough to provide the home link) atm but I have a problem with product listing pages and the tips and tricks page, whereby what seems to be happening is a complete failing on the part of jMenu and jFlipBook respectively.
Since I did not write the original site nor have I used these plugins before I'm not sure why they're not working properly. You'll notice on the tips and tricks section if you inspect element and set the nav items to include the class jMenu (which jMenu should do automatically) the page becomes about 90% fixed (save for the submenus still being out of action). After digging around in the source for a while I've come to a dead end, so if anyone can shed any light on the matter it would be really helpful! Cheers in advance!
If you look at the javascript console you will see errors pointing to your document ready code blocks.
You are using:
$('document').ready(function(){
when it should be:
$(document).ready(function(){
I would imagine that this is breaking your code.

jquery click function in for loop only working in last loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I'm probably doing something obviously wrong, but I just can't see it. I'm trying to use a for loop to define multiple click events and am experiencing an unexpected result. Some of this is working (the hide and show at the beginning of the function, but both sections wind up targeting the second item in the loop. Could somebody take a look at this and tell me what I'm doing wrong? Thank you so much for your help!
here is the link:
http://grana.us/test/expand2.html
You are assigning same event to all summaries for every id. This is wrong...
First... to hide all details and show all toglers simply use:
$('.details').hide();
$('.toggler').show();
And then define click function to all sumaries:
$('.summary').click(function(){
if($('.toggler',this).html() == ' -'){
$('.toggler',this).html(' +');
$('.details',$(this).parent()).hide();
}else{
$('.toggler',this).html(' -');
$('.details',$(this).parent()).show();
}
});
Put everything in...
$(function(){
...
});
and should be ok.

Categories