0

I have the following elements

<li data-attr="sessionContext='test session'"></li>
<li data-attr="sessionContext='test session 12345'"></li>

// I want to get all elements with [data-attr=someContext*=] -doesn't 
matter someContext= value, but doesn't work
$("[data-attr]") // gets all elements, but not sessionContext only
$("[data-attr='sessionContext=test session']") // works but for single element only
1
  • Use the attribute contains selector *= properly. Check the jquery documentation. Commented Dec 23, 2020 at 13:40

2 Answers 2

4

Using a selector, you can try this:

$('[data-attr="sessionContext=test session"]')     
// will return the element sessionContext=test session

$('[data-attr^="sessionContext=test session"]' )   
// will return all that begin with sessionContext=test session

$('[data-attr$="sessionContext=test session"]' )   
// will return all that end with sessionContext=test session

$('[data-attr*="sessionContext=test session"]' )   
// will return all that contain sessionContext=test session
Sign up to request clarification or add additional context in comments.

Comments

-2

Try this:

$("[data-attr^=sessionContext]")

^ will match anything starting with sessionContext

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

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.