Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If element exist then check?

The code does work below when the access to the webpage, it automatically hide #OrderDeliveryAddress div. But I am wondering is this correct way doing it?

Is there a way to check if .selectAddressList div/class exist first and then check the value?

$(document).ready(function() {
    if ($(".selectAddressList").val() == "selectAddressBook") {
         $("#OrderDeliveryAddress").hide();
    }
});
like image 885
user622378 Avatar asked Apr 27 '11 17:04

user622378


People also ask

How do you check if an element exists in a list?

We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.

How do you check if a element is present or not in DOM?

contains DOM API, you can check for the presence of any element in the page (currently in the DOM) quite easily: document. body. contains(YOUR_ELEMENT_HERE);

How do I check if an element is present in a Web page?

Using driver. In order to check if an element is present on a webpage, we make use of driver. findElements() method. As we know that driver. findElements() method returns a list of webElements located by the “By Locator” passed as parameter.


2 Answers

Personally I would use:

if ($(".selectAddressList").length > 0)

This checks if the jQuery object has any items, in other words if anything matched the selector you passed in.

like image 124
justkt Avatar answered Sep 21 '22 18:09

justkt


if($(".selectAddressList").length > 0)

At a second glance though, you're using a class selector for this - do you have multiple items using this class on the page? If so, you might run into conflicts there as you're checking the .val() of it/them. If not, you might consider using element id as opposed to class.

like image 40
Demian Brecht Avatar answered Sep 19 '22 18:09

Demian Brecht