6

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";
    }
}
5
  • 16
    please try to do it by yourself first Commented Sep 12, 2013 at 5:51
  • What actually you trying to doing using JavaScript? Commented Sep 12, 2013 at 5:52
  • There are a lot of things that you can be told on this, but I think you need to start learning jQuery rather than asking questions. This should be a better place for starting jQuery (api.jquery.com/category/core) Commented Sep 12, 2013 at 5:57
  • @NaveedButt the specific question is how to convert getElementByName. While a tutorial is a good idea, this question does not appear too broad to me. Commented Sep 12, 2013 at 5:58
  • 2
    -> api.jquery.com/category/selectors Commented Sep 12, 2013 at 6:00

4 Answers 4

4

you can try with attribute selector...

 $('[name="'+name+'"]')  //for name selector
 $('.visible') //for class selector
Sign up to request clarification or add additional context in comments.

1 Comment

glad it helped.... you can accept any of the answer if you think that helped... anyways happy coding.. :)
3

You should try because when you try practicing you actually are learning:

  • document.getElementsByName in jQuery $("[name=Name]");
  • document.getElementsById in jQuery $("#IdofElement");
  • document.getElementsByClass in jQuery $(".ClassofElement");

Here are some tutorials:

  1. jQuery for Beginners
  2. jQuery API
  3. Beginners Guide to jQuery

2 Comments

Why are you not linking to the official jQuery tutorial (I don't mean the documentation)?
@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.
0

You can refer to the below syntax for your reference.

$('[name=tcol1]') // matches exactly 'tcol1'

$('[name^=tcol]') // matches those that begin with 'tcol'

Comments

0

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");
    }
}

4 Comments

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.
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");
Nevermind, Barmar is right, it ends up with a hidden and visible class.
I added .removeClass("visible") to the end of line six and it worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.