Reputation: 1469
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 his login name and clicks on submit. After that, if he has set a challenge question, it will be shown and he will be able to answer this question and submits again. (same button before).
My problem:
when the user clicks on submit, in IE, if he clicks on it several times, he'll get one e-mail for each time he clicks on it.
What I think:
I want to disable the button after clicking on this submit button, but I can only disable it if two conditions are correct:
I cannot change the process this is done, so I thought about adding an id in the field of the answer and checks if it's visible. if it is and the user clicks on the submit button, I want to apply the attribute disable button on the label. What I don't know is how to do this without using jquery.
with jQuery I could do something like this:
if($('#secretAns').is(':visible')) {
//i think it could be the solution
$('#general_Submit.Label').attr( disabled, disabled );
}
to apply on:
<div id="secretAns" class="loginTxtFieldWrapper">
<font color='red'>*</font><input type="text" name="secretAns" />
<input type="hidden" name="isAnswerPage" value="1"/>
</div>
<p id="loginSubmitLink">
<input id="general_Submit.Label" type="submit" value="general_Submit.Label" />" />
</p>
I find hard to search for pure Javascript solutions, because everybody tends to use jQuery, and I can't use it in my application, so if someone can help me to do this with pure Javascript, I'll appreciate.
Upvotes: 16
Views: 10679
Reputation: 1616
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.
function isVisible(el) {
while (el) {
if (el === document) {
return true;
}
var $style = window.getComputedStyle(el, null);
if (!el) {
return false;
} else if (!$style) {
return false;
} else if ($style.display === 'none') {
return false;
} else if ($style.visibility === 'hidden') {
return false;
} else if (+$style.opacity === 0) {
return false;
} else if (($style.display === 'block' || $style.display === 'inline-block') &&
$style.height === '0px' && $style.overflow === 'hidden') {
return false;
} else {
return $style.position === 'fixed' || isVisible(el.parentNode);
}
}
}
Upvotes: 1
Reputation: 3281
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" />" />
function Callfun()
{
document.getElementById("general_Submit.Label").disabled = true;
}
Upvotes: 0
Reputation: 150303
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.
Searching the source code gave me this:
// The way jQuery detect hidden elements, and the isVisible just adds "!".
elem.offsetWidth === 0 && elem.offsetHeight === 0
Upvotes: 27
Reputation: 1469
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
function avoidDoubleSubmit() {
<c:if test="${not empty secretQues}">
event_addEvent(window,'load',function(){document.ResetPasswordForm.secretAns.focus();});
document.getElementById('general_Submit.Label').style.display ='none';
</c:if>
}
Upvotes: 1
Reputation: 1114
Please see this it's customary seldom code might be useful
<!DOCTYPE html>
<html>
<body>
<div id="one" style="visibility: hidden;">The content of the body element is displayed in your browser.
</div>
<div id="two" style="visibility: visible;"> I'm Cool
</div>
<div onclick="push();"> Click me </div>
<script>
function push() {
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.
Upvotes: 1