Click here to Skip to main content
15,889,840 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Experts,

I am just starting to learn JavaScript. And I'm currently working on a project that only JavaScript is accepted. The project is to toggle multiple buttons and have found the code solution for this but it's in jQuery. It's working fine... Demo is at http://jsfiddle.net/ladyblue1075/rvhmkpu5/3/[^]

But I need the code function written in JavaScript instead of jQuery. Can anyone help me how to convert this jQuery function into JavaScript code?

Thank you!

C#
jQuery(document).ready(function(){

    // INT
    // the height of the highest one
    var highestlanguage = 0;

    // get highest language so it will not scroll up.
    var _gethighestlanguage = function () {
        var _height = jQuery(this).height();
        if(highestlanguage<_height) {
            highestlanguage = _height;
        }
    };

    // It's not a "real toggle". Just close all and slide down the one
    var _togglelanguage = function(){
        var _target = jQuery(this).data('target');
        jQuery(".language").hide();
        jQuery("#"+_target).slideDown();
    };

    // close the nearest element with the class "language". So don't use it in these boxes again
    var _closelanguage = function () {
        jQuery(this).closest('.language').slideUp();
    };

    // Get highest language
    jQuery(".language").each(_gethighestlanguage);

    // Assign height to the container. (Prevents page from up-scrolling on close)
    jQuery(".container").css("min-height", highestlanguage + "px");

    // Hide All
    jQuery(".language").hide();

    // Assign functions to the buttons
    jQuery('.toggle').click(_togglelanguage);
    jQuery(".close").click(_closelanguage);
});
Posted

1 solution

Why? Do you really understand that jQuery is also Javascript? So you cannot reasonably mention "only JavaScript is accepted"; this is totally irrelevant; in all cases, we discuss Javascript and nothing else.

So, instead of eliminating jQuery, why not, for example, putting it on the site. It would not mean "installing" anything. jQuery is just put on the site in exact same way your Javascript (you are discussing in your question) is put.

So, there is no "conversion"; it sounds just like nonsense. Likewise, "Javascript instead of jQuery" is nonsense. Please start with understanding of this point. If, by some weird reason, you want to eliminate jQuery (why?!), just don't use it (but why asking this question then?). What's the problem? You don't know how to hide and show the element, slide it, and so on? Then learn it from the documentation and ask your particular questions if you face some problems (please don't expect that someone does your work for you; we usually don't do it). Just do the job (but why doing this job if it is already done for you in jQuery?!).

How else? You know what, if you have nothing else to do, just look at jQuery code. All the answers to your questions are there. Or some other code. :-)

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900