Skip to content Skip to sidebar Skip to footer

Detect If An Element Is Visible (without Using Jquery)

I'm trying to detect if an html element I gave an id is visible or not without using jquery. The context: In the forgotten user password page, I have a form where the user enter hi

Solution 1:

Google helped me finding out how jQuery does it, you can do the same:

In jQuery 1.3.2 an element is visible if its browser-reported offsetWidth or offsetHeight is greater than 0.

Release notes

Searching the source code gave me this:

// The way jQuery detect hidden elements, and the isVisible just adds "!".
elem.offsetWidth === 0 && elem.offsetHeight === 0

Solution 2:

Please see this it's customary seldom code might be useful

<!DOCTYPE html><html><body><divid="one"style="visibility: hidden;">The content of the body element is displayed in your browser.
</div><divid="two"style="visibility: visible;"> I'm Cool
</div><divonclick="push();"> Click me </div><script>functionpush() {
a = document.getElementById("one").style.visibility;
alert("one "+a);

b = document.getElementById("two").style.visibility;
alert("one "+b);
}
</script></body></html>

above code gives the visibility status of the div using it's ID as U required.

Solution 3:

As I was thinking about disabling the button, I'm here to write that I was wrong about my idea of solution for this problem, because it would be really costly to make it works. This way, after studying, testing some ideas and reading the ideas @gdoron and @AmeyaRote brought, I figured out that a simple solution was to hide the 'submit' button after clicking on it (the page is refreshed, the validation is checked and the button is available again after this process). So, here is the solution to solve the problem to avoid the user of clicking more than once on the submit button, for this particular case where I cannot disable it (if I disable after clicking, the form was not being submited):

The HTML

I just added the onclick attribute calling a javascript function I created to hide the button:

<p id="loginSubmitLink">
    <input id="general_Submit.Label"type="submit" onclick ="avoidDoubleSubmit();" value="general_Submit.Label" />" />
</p>

The Javascript

functionavoidDoubleSubmit() {
    <c:iftest="${not empty secretQues}">
        event_addEvent(window,'load',function(){document.ResetPasswordForm.secretAns.focus();});
        document.getElementById('general_Submit.Label').style.display ='none';
    </c:if>
}

Solution 4:

I wrote this some time back. This handles a lot of cases to my knowledge. This actually checks if the element is being seen by the user in it's current state.

Checks for display, visibility, opacity, overflow quirk and does it recursively till we go till the document node.

functionisVisible(el) {
        while (el) {
            if (el === document) {
                returntrue;
            }

            var$style = window.getComputedStyle(el, null);

            if (!el) {
                returnfalse;
            } elseif (!$style) {
                returnfalse;
            } elseif ($style.display === 'none') {
                returnfalse;
            } elseif ($style.visibility === 'hidden') {
                returnfalse;
            } elseif (+$style.opacity === 0) {
                returnfalse;
            } elseif (($style.display === 'block' || $style.display === 'inline-block') &&
                $style.height === '0px' && $style.overflow === 'hidden') {
                returnfalse;
            } else {
                return$style.position === 'fixed' || isVisible(el.parentNode);
            }
        }
    }

Solution 5:

On general_Submit.Label button click call a function Callfun() and then disabled button

<input id="general_Submit.Label" onclick="Callfun()"type="submit" value="<ezmi18n:message key="general_Submit.Label" />" />

    functionCallfun()
    {
     document.getElementById("general_Submit.Label").disabled = true;
    }

Post a Comment for "Detect If An Element Is Visible (without Using Jquery)"