Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i mean i wanna iterate manually using a for-loop or something. but this piece of code i came up with seems to be not working. i like combining javascript with jquery since jquery is not my cup of tea for major projects. i don't know much jquery either, i would say I'm beginning to learn, though. how do you iterate over a nodelist in jquery is the question i have for all those jquery fans this time. is it similar to the javascript way? anyway this is what i have come up with (the code of a beginner).
$("sn"[i]).fadeIn();
$("sn"[i]) the part which failed, according to google chrome.
try this:
$("sn[" + i + "]").fadeIn();
I think you mean that "sn" is the selector for the nodes, in that case:
$("sn").fadeIn();
This works on all the elements that match the selector, jQuery will do the iteration. However if you want to select all elements that have the 'sn' class you should prefix the selector with a . like so: ".sn"
if you want to loop manually try:
$(".sn").each(function(i) {
$(this) // do some magic with the individual element here
});
See more on iterating with each here:
https://api.jquery.com/each/
Assuming sn is a variable containing the node list, you are probably looking for
$(sn[i])
or
sn.eq(i)
if sn is already a jQuery object.
Related
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having an issue at work with a rather picky client.
We use a rather strange script to deal with something that they require. We're an online assessment company and this script in general applies in our test player, rather than in our question editor, so we can't make change in the html, as you normally would with a text input box.
I know the maxlength attribute can be added through use of jQuery by using something along the lines of
$("input").prop("maxLength", 3)
However, I do not know how I would reference this in the HTML, as it would only be used in a couple of questions that use this script so making it standard for these questions by adding it to the JavaScript used is not an option.
The inputs that you need to apply this to would need to have ID's or Classes.
As you cannot edit the HTML this would only work, if those input's had ID's or Classes already.
If you want to apply it to an element with an Id you would do:
$("#IDGOESHERE").prop("maxLength", 3);
http://api.jquery.com/id-selector/
If it was with a class :
$(".CLASSNAMEHERE").prop("maxLength", 3);
http://api.jquery.com/class-selector/
You could get a little more fancy, by using EQ,
$("input").eq(5).prop("maxLength", 3);
Which would apply the max length to the 5th input on the page.
http://api.jquery.com/eq/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I go through a certain ul-Element and search every li-Element in it? I've only found jquery solutions, but don't want to use it here.
Looping through a selection without some external library such as jQuery or D3 is kind of annoying because the selection will not return an Array but instead an HTMLCollection or NodeList (depending on your browser). I recommend using some third party library if you are doing this sort of thing often. Anyway here's how to do it in pure JavaScript.
// This is not an Array but either an HTMLCollection or a NodeList
var selection = document.getElementById(<id>).getElementsByTagName("li");
for(var i = 0; i < selection.length; i++)
{
// do something with selection[i]
}
It is important to note that because your selection is not an Array you cannot use for-each loops (the intended way at least) or any higher level Array functions as described here.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to phonegap.So,using JAVASCRIPT for the first time.I am working in javascript. I am comfortable working with Java Script. I am implementing a plugin for SMS in Phone Gap.
In the tutorial they have asked to use the following code.
window.plugins.sms.send($('#phone').val(),
$('#message').val(),
function () {
alert('Message sent successfully');
},
function (e) {
alert('Message Failed:' + e);
}
);
Could some one help me out in de-coding the jquery code?
please help me out.
You can use document.getElementById('phone').value instead of $('#phone').val() to get element's value without jquery.
It looks like that function window.plugins.sms.send takes a phone number, a message to send, a function to run on success, and a function to run on failure. What that code is doing is providing the first 2 of those 4 things using jQuery selectors, and the last 2 as anonymous functions.
Have a look at http://jquery.com/ (yes, just the homepage) and scroll down to the "A Brief Look" section. That will give you an idea of what the code you provided is doing.
Basically, the tricky bit to get your head around is that $ in there.
The $ in jQuery is just the function that instantiates jQuery. Anything in the parentheses immediately after it is a selector, and then everything after that are methods to call on the object returned by the selector.
The code you provided is looking through the DOM for an element called phone and an element called message. The # in front of those means to match only one, unique element. jQuery selectors can also be used to select groups of elements and operate on them all at once.
I'd really recommend going through the jQuery tutorials as they are quite quick to get through. If you already know javascript you'll have little problem getting your head around it.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Yesterday I have put the question which was answered correctly but now I want to convert that same code into javascript. I have made some changes but its not work
working code- http://jsfiddle.net/SXzyR/8/
mycode- http://jsfiddle.net/SXzyR/11/
You want to use this.id instead of document.getElementById(this) to get the id string where this is a DOM element.
Please read some tutorials about jQuery. This is a good start, also have a look here.
jQuery isn't another language, it's a library for JavaScript. Considering the following line from your "translated" code:
txtval(document.getElementById(this)
Instead of writing document.getElementById you can simply use jQuery to write
txtval($(this))
as in the first example (working code).
Also you are mixing jQuery with "native" JavaScript/DOM functions in your code. Don't reinvent the wheel, use jQuery to accomplish your task.