I have this JavaScript functions that I want to convert to jquery but I can't understand jquery. It uses the document.getElementsByName a lot so I would like to know how to convert the document.getElementsByName into jquery.
function getElements(name){
if (document.getElementsByName(name)[0].className == "visible"){
document.getElementsByName(name)[0].className = "hidden";
} else {
if(document.getElementsByClassName('visible')[0] != null){
document.getElementsByClassName('visible')[0].className = "hidden";
}
document.getElementsByName(name)[0].className = "visible";
}
}
ssilas777
9,7704 gold badges48 silver badges70 bronze badges
asked Sep 12, 2013 at 5:50
Eddie Castle
811 gold badge1 silver badge5 bronze badges
4 Answers 4
you can try with attribute selector...
$('[name="'+name+'"]') //for name selector
$('.visible') //for class selector
answered Sep 12, 2013 at 5:53
bipen
36.6k9 gold badges51 silver badges62 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
bipen
glad it helped.... you can accept any of the answer if you think that helped... anyways happy coding.. :)
You should try because when you try practicing you actually are learning:
document.getElementsByNamein jQuery$("[name=Name]");document.getElementsByIdin jQuery$("#IdofElement");document.getElementsByClassin jQuery$(".ClassofElement");
Here are some tutorials:
answered Sep 12, 2013 at 5:55
Zaheer Ahmed
28.7k12 gold badges77 silver badges113 bronze badges
2 Comments
John Dvorak
Why are you not linking to the official jQuery tutorial (I don't mean the documentation)?
iConnor
@EddieCastle You would go to google, and search "how to get elements by name jQuery" and then you would search "How to get elements by class name jQuery" then you would search "how to add a class to a element jQuery" Then you would put together what you have learned to create your functionality.
You can refer to the below syntax for your reference.
$('[name=tcol1]') // matches exactly 'tcol1'
$('[name^=tcol]') // matches those that begin with 'tcol'
answered Sep 12, 2013 at 5:52
Ganesh Pandhere
1,6528 silver badges12 bronze badges
Comments
Try this.
function getElements(name1){
if ($("[name='" + name1 + "'").hasClass("visible")){
$("[name='" + name1 + "'").removeClass("visible").addClass("hidden");
} else {
if($(".visible") != null){
$(".visible").addClass("hidden");
}
$("[name='" + name1 + "'").removeClass("visible").addClass("visible");
}
}
answered Sep 12, 2013 at 5:57
sudhansu63
6,2204 gold badges41 silver badges55 bronze badges
4 Comments
Barmar
His original code replaces the class, you're adding a class without removing the old one. You'll end up with an element that's both hidden and visible, which is probably not desired.
Eddie Castle
This is exactly how I wanted the code thanks, with the exception that on line 8 it would be $("[name='" + name1 + "'").removeClass("hidden").addClass("visible");
Eddie Castle
Nevermind, Barmar is right, it ends up with a hidden and visible class.
Eddie Castle
I added .removeClass("visible") to the end of line six and it worked
lang-js
getElementByName. While a tutorial is a good idea, this question does not appear too broad to me.->api.jquery.com/category/selectors