Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting javascript to jquery

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";
    }
}
like image 526
Eddie Castle Avatar asked Sep 12 '13 05:09

Eddie Castle


People also ask

Can you use normal JavaScript with jQuery?

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery.

Is it easy to convert JavaScript to TypeScript?

Converting a JavaScript codebase over to TypeScript is, while somewhat tedious, usually not challenging. In this tutorial, we're going to look at how you might start out. We assume you've read enough of the handbook to write new TypeScript code.

What is $( this in JavaScript?

“This” keyword refers to an object that is executing the current piece of code. It references the object that is executing the current function. If the function being referenced is a regular function, “this” references the global object.


2 Answers

you can try with attribute selector...

 $('[name="'+name+'"]')  //for name selector
 $('.visible') //for class selector
like image 133
bipen Avatar answered Oct 19 '22 22:10

bipen


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
like image 31
Zaheer Ahmed Avatar answered Oct 19 '22 22:10

Zaheer Ahmed