0

I am trying to check value for an <html> tag's attribute using following jquery code:

if ($('[data-theme == "dark"]')) {
        $(".epcBanner").css("url", "(~/Content/Images/EPC-.png)");
    }

HTML

<html data-theme="dark>

All I'm trying to check is data-theme's value and above code is not working. How can this be achieved. Thank you in advance.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • `%( )` returns an object. And any object is always "truthy". You have to check if that jQuery object contains an actual element. – Andreas Aug 05 '22 at 09:24
  • 1
    Does this answer your question? [Check if element exists in jQuery](https://stackoverflow.com/questions/4592493/check-if-element-exists-in-jquery) – Andreas Aug 05 '22 at 09:25
  • I am not checking if element exists or not. I am trying to check element's attribute value equal to something or not. Your link is wrong – user19698365 Aug 05 '22 at 10:03
  • Right now you only have an invalid CSS selector - but with the description my dupe target is still valid. _"I am trying to check element's attribute value."_ -> What element? -> [mcve] – Andreas Aug 05 '22 at 10:06
  • I have edited my question. Hope it answers your question – user19698365 Aug 05 '22 at 10:09
  • Then select the `` tag and then use `.data()` – Andreas Aug 05 '22 at 10:11
  • https://api.jquery.com/attribute-equals-selector/ – ADyson Nov 07 '22 at 08:11

1 Answers1

0

you should check if the element exists, so the condition should be

if ($('[data-theme == "dark"]').length > 0)

and there is no url property: if you are setting a background the property is

$(".epcBanner").css("background-image", "url(~/Content/Images/EPC-.png)");
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177