Arjun Sah
Arjun Sah

Reputation: 77

How to get Elements from a custom attribute in Javascript?

I want to extract the text value with javaScript from this below HTML:

<div class="search-box-compact__label text-color-secondary">
   <span data-bind="i18n: 'search.keyword-label'">Find Jobs</span>
</div>

I have tried this but its not working. Kindly suggest a solution:

  let y = document.querySelectorAll("[data-bind=`i18n: 'search.keyword-label'`]");

I am getting the following errors::

Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Document': '[data-bind=`i18n: 'search.keyword-label'`]' is not a valid selector.

Kindly suggest.

Upvotes: 2

Views: 1887

Answers (1)

Simone Rossaini
Simone Rossaini

Reputation: 8162

You need to escape ', i used " too

let y = document.querySelectorAll("[data-bind=\"i18n: \'search.keyword-label\'\"]");
console.log(y);
<div class="search-box-compact__label text-color-secondary">
   <span data-bind="i18n: 'search.keyword-label'">Find Jobs</span>
</div>

Upvotes: 3

Related Questions