1

Given the element below ("FileToUpload"), how do I test if its class is set to block or none?

HTML

<div id=\ "FileToUploadLabel\">File:</td>
    <td colspan=\ "1\">
</div>
<input type=\ "file\" id=\ "FileToUpload\" name=\ "FileToUpload\" size=\ "70\"/>

JS

//To show the file control: 
$("#FileToUpload").css("display", "block");

//To hide the file control: 
$("#FileToUpload").css("display", "none");

Basically I need to test it in a conditional statement like this:

if ("FileToUpload not hidden" && $("#FileToUpload").val() == "") 
{
  Alert("An file needs to be uploaded when File tag is being displayed.");
  return;
}

Thank you

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Max
  • 1,289
  • 3
  • 26
  • 50

4 Answers4

2

easiest way is to use the built in ":visible" selector in jquery

if($("#FileToUpload").is(":visible") && $("#FileToUpload").val() == ""){
  //do something
}
Ian
  • 326
  • 2
  • 8
  • Sure thing! And as someone else mentioned, using jquery's built in `hide()` and `show()` is simpler than using `css('display', 'block')` to show and hide elements @Max – Ian Aug 07 '13 at 22:32
1

See this StackOverflow question. Use $(element).is(":visible") to check if it is hidden or not. Also, you can use .hide() and .show() to more easily hide and show elements.

Community
  • 1
  • 1
ShadowCat7
  • 804
  • 8
  • 16
1

something like this should work for you:

var currDisplay = $("#FileToUpload").css("display");


if (currDisplay != "none" && $("#FileToUpload").val() == "") 
{
  Alert("An file needs to be uploaded when File tag is being displayed.");

}
Rooster
  • 9,954
  • 8
  • 44
  • 71
0

As seen on this answer You can use the :hidden selector:

if($('#FileToUpload:hidden').length && $("#FileToUpload").val()){
    Alert("An file needs to be uploaded when File tag is being displayed.");
    return;
}
Community
  • 1
  • 1
DarkAjax
  • 15,955
  • 11
  • 53
  • 65