0

So I have the following html object, I want to select it based on the data-value

[<span class="selecter-item selected" data-value="CS 245">CS 245</span>]

I tried something like this, but it does not work.

$("span[data-value='CS 245']")

Anybody knows how to do it?

2
  • Did you say object? As it is you selector is targeting the DOM, not some object. Commented Nov 18, 2014 at 1:27
  • I don't see anything wrong in the selector, except for the empty space at the end, check my answer Commented Nov 18, 2014 at 1:30

3 Answers 3

1

There are two issues:

  1. You have a space after 245
  2. When I copied and pasted your HTML there was a character, I had to delete after =.

This clearly shows you may want to cleanup your HTML to avoid such issues.

The demo works fine.

alert( $("span[data-value='CS 245 ']").length );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<span class=​"selecter-item selected" data-value="CS 245 ">​CS 245​</span>​

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

2 Comments

sorry yeah, I was not expecting that you copy over my code and test it. Its my bad. I will edit my question right now.
And why do you have square brackets around your markup? What object are you talking about?
0

If you want to know the value of data-value:

$("span").attr("data-value");

If you want to set the data_value, as shaN said:

$("span").attr("data-value","CS 245");

But if you want to select as you did try to delete the empty space data-value=​"CS 245 "

Comments

0

You have data-value=​"CS 245 " (Note the empty space!) you should have then :

$("span[data-value='CS 245 ']")

Or just remove the empty space :) and should work the way you currently have it

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.