25

how can i check a if an element is visible or hidden with jquery and perform some action?

below given is my form related code,

<form>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Full name: <input type="text" name="fullname"><br>
DOB: <input type="text" name="dob">
Address: <input type="text" name="address">
</form>

i need to hide the full name text field when first name text field or last name text field is displaying.

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96

5 Answers5

8

try something like this

if($('#testElement').is(':visible')){
   //what you want to do when is visible
}

for your code

if($('input[name="firstname"], input[name="lastname"]').is(':visible')){
  $('input[name="fullname"]').hide();
}

REFERENCE

http://api.jquery.com/visible-selector/

rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
1
if($('input[name="firstname"], input[name="lastname"]').is(':visible') === true)
    $('input[name="fullname"]').hide();
Vishal
  • 1,199
  • 1
  • 8
  • 13
0

you should change

<input type="text" name="fullname"> 
to 
<input type="hidden" name="fullname">

to make an input field hidden

Priya jain
  • 753
  • 2
  • 5
  • 15
0

this should work $(element).is(":visible")

semirturgay
  • 4,151
  • 3
  • 30
  • 50
0

I don't know the logic behind your question, but this demo should do the trick. DEMO

$(document).ready(function(){
if($('#firstname').is(':visible') || $('#lastname').is(':visible'))
    $('#fullname').parent().hide();

})

I added some parent divs to hide the text and the input on once. If you want you can wrap the text in label tag for more clearly output.

NoSense
  • 949
  • 2
  • 11
  • 21