2

I am trying to get the 31003 value from the attribute with jquery.

 <a class= "findOption" data-clickaction="changeFilter" data-actionvalue=" {"Company":"31003", "val": "mostrecent";}" href="javascript:void(0);">Most Recent</a>

I have tried,

$(".findOptions").attr('Company').value();

$(".findOptions").attr('data-actionvalue');

I am stuck.

3
  • Company is part of the string that is the value of data-actionvalue. Company is itself not an attribute. So you would need to do some searching in that string. It is also invalid HTML because you have double double quotes. Commented Jun 26, 2015 at 22:19
  • Avoid using double quotes here and there.... Distinguish them using single quotes and double quotes properly.. Commented Jun 26, 2015 at 22:20
  • I think you will need to use single quotes as the data-actiionvalue attribute will read as data-actionvalue=" {" and Company":" will try act like another attribute. " < used to block in / open close the attribute data. Commented Jun 26, 2015 at 22:21

4 Answers 4

3

You can use data method:

$('.findOption').data('actionvalue')['Company'];//findOption instead as noticed by @Vimalan

And as @Barmar indicated that html is invalid you should be doing like this in your html:

data-actionvalue='{"Company":"31003", "val": "mostrecent"}'
Sign up to request clarification or add additional context in comments.

Comments

1

First, your HTML isn't correct. Since you're using double quotes in the JSON that's in data-actionvalue, you have to use single quotes as the delimiter around the attribute value. Otherwise, the quote before Company will end the attribute.

Also, you shouldn't have a semicolon (;)` in the value, that's not valid JSON.

<a class= "findOption" data-clickaction="changeFilter" data-actionvalue='{"Company":"31003", "val": "mostrecent"}' href="javascript:void(0);">Most Recent</a>

Then to get a particular property of the data, you need to access it with .:

$('.findOption').data('actionvalue').Company

or []:

$('.findOption').data('actionvalue')['Company']

8 Comments

Great answer but i'm just curious. If this anchor tag is dynamically created won't it use double quotes by default?
@NewToJS It doesn't matter.
@NewToJS If it's created dynamically, then it doesn't use quotes at all, that's just part of the HTML syntax. If you create it dynamically you're just manipulating the DOM as objects.
@BhojendraNepal I'm asking our of curiosity.
@NewToJS If you use without quote then also it works.
|
1

There are couple of corrections in your code:

1) Fix, 'actionvalue' as shown in my code. 2) Use findOption instead of findOptions (it does not exists in your code)

var obj = $(".findOption").data('actionvalue');
$('#result').text(obj.Company);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class= "findOption" data-clickaction="changeFilter" data-actionvalue='{"Company":"31003", "val": "mostrecent"}' href="javascript:void(0);">Most Recent</a>

<br/>
<label id='result'/>

3 Comments

Woah, that eval() is completely unnecessary.
@Ja͢ck, $('.findOption').data('actionvalue')['Company'] or $('.findOption').data('actionvalue').Company is not working. So, I opted to use eval to get the result.
Doesn't work because it's not recognised as JSON because you're using single quotes for the keys and values ... instead, you should use double quotes inside and use single quotes for the HTML attribute.
0

I think this should work

$(".findOption").data('actionvalue')['Company'];

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.