Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of .show() - jQuery in plain JavaScript

Anyone has any idea whats the equivalent of.show() of jquery in javascript?

I have tried using document.getElementById and add / remove class that I named "show" // "hide" but that doesn't work very well I might have wrong attributes in those classes I am not sure..

the show class is :

.show{
    position:relative;
    display:block;
}

and the hide class is:

.hide{
    position:absolute;
    top:0px;
    right:0px;
    display:none;
}

I am pretty sure there is a better way of going so.

like image 322
Neta Meta Avatar asked Aug 15 '12 05:08

Neta Meta


People also ask

What does show () do in jQuery?

jQuery Effect show() Method The show() method shows the hidden, selected elements. Note: show() works on elements hidden with jQuery methods and display:none in CSS (but not visibility:hidden).

Is jQuery hide () is equivalent to?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.

What is equivalent to jQuery?

The equivalent to $() or jQuery() in JavaScript is querySelector() or querySelectorAll() , which can be called with a CSS selector.

What is $( this in JavaScript?

$(this) is a jQuery object and this is a pure DOM Element object. See this example: $(".test"). click(function(){ alert($(this). text()); //and alert(this.


2 Answers

document.getElementById('myElement').style.display = 'block'; // show
document.getElementById('myElement').style.display = 'none'; // hide
like image 71
mattnull Avatar answered Oct 08 '22 02:10

mattnull


jQuery takes into account what the value of display was before hiding. When you fire show(), it returns it back to what that value was. And so, it's NOT just simply setting the element's display property to block and none.

so basically:

function hide(){
    //get previous display value
    //store it in an internal cache. jQuery has an internal data storage
    //hide element
}

function show(){
    //get previous display value for that element
    //apply to element to show it
}

.show()

The matched elements will be revealed immediately, with no animation. This is roughly equivalent to calling .css('display', 'block'), except that the display property is restored to whatever it was initially. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

like image 42
Joseph Avatar answered Oct 08 '22 04:10

Joseph