0

Getting Uncaught Error: Syntax error, unrecognized expression: data-key['lifetimeStays']

want to get value from data-key="lifetimeStays" and data-key="lifetimeNights"

HTML:

<div data-cookies="profile-cred" data-key="lifetimeStays" class="value cookies-value">1110</div>

<div data-cookies="profile-cred" data-key="lifetimeNights" class="value cookies-value">9000</div>

JS:

$("data-key['lifetimeStays']").text();
$("data-key['lifetimeNights']").text();
1

2 Answers 2

1

use $("[data-key='lifetimeStays']").text();. data-key is an attribute https://api.jquery.com/attribute-contains-prefix-selector/.

console.log($("[data-key='lifetimeStays']").text());
console.log($("[data-key='lifetimeNights']").text());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-cookies="profile-cred" data-key="lifetimeStays" class="value cookies-value">1110</div>

<div data-cookies="profile-cred" data-key="lifetimeNights" class="value cookies-value">9000</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Your code $("data-key['lifetimeStays']") means you select element data-key having attribute lifetimeStays, namely this element <data-key lifetimeStays/>.

Try this to select attribute which has specific value

$('[data-key=lifetimeStays]').text()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.