0

I'm trying to check to see if an div called .errormsg exists and if it does then I don't want anything to happen but if it doesn't exist i want my form to redirect to a new URL after it. This is what I've done but I'm not sure it's correct at all, I'm only learning so any help would be much appreciated

thanks

user3620201
  • 9
  • 1
  • 3

3 Answers3

3

Check with a length property of a jQuery object

if($('div.errormsg').length==0)
{
 //Not exists, so redirect
 //window.location.href='newurl';
}
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
1

change this:

if (document.getElementById('.errormsg')) {

to this:

if (document.getElementsByClassName('errormsg').length) {

document.getElementsByClassName docs @ MDN

Jai
  • 74,255
  • 12
  • 74
  • 103
0

You can use .length property to check the existance of element/elements:

if(!$('.errormsg ').length){//does ot exist
  //redirect
}
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125