-2

say i have an element that I want to convey 2 or more pieces of data with a single click....

<a class="quickie" data="['jan',1]" >week 1</a>
<a class="quickie" data="['jan',2]" >week 2</a>
<a class="quickie" data="['jan',3]" >week 3</a>
...and so forth.

$( '.quickie' ).click( function() {
        var dataArray = JSON.stringify( $( this ).attr( 'data' ) );
        var month = dataArray[0];
        var week = dataArray[1];
});

This seems to treat the entire string as an array with each character being its own element.?? I thought "JSON.stringify()" would solve this, but it doesn't.

Help please.either straight javascript, or jquery would do.

2
  • 1
    It's JSON.parse(). Here, you are converting a String to... a String. Commented Jun 28, 2015 at 19:00
  • possible duplicate of Difference between JSON.stringify and JSON.parse Commented Jun 28, 2015 at 19:05

3 Answers 3

4

JSON.stringify converts an object to a JSON string.

You have a JSON string in the data attribute that you want to parse as a JavaScript object.

Use JSON.parse

var dataArray = JSON.parse( $( this ).attr( 'data' ) );
Sign up to request clarification or add additional context in comments.

1 Comment

SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON datavar dataArray = JSON.parse( $( this ).attr( 'data' ) );
3

html at

data="['jan',1]"

returns

Uncaught SyntaxError: Unexpected token ' 

try substituting

<a class="quickie" data='["jan",1]'>week 1</a>
<a class="quickie" data='["jan",2]'>week 2</a>
<a class="quickie" data='["jan",3]'>week 3</a>

for

<a class="quickie" data="['jan',1]" >week 1</a>
<a class="quickie" data="['jan',2]" >week 2</a>
<a class="quickie" data="['jan',3]" >week 3</a>

additionally, appear to be syntax error at close of .click() handler; try substituting

});

for

)};

utilizing JSON.parse to parse string at .data() , instead of JSON.stringify , which returns string representation of object , not object parsed from string at data-* attribute


$(".quickie").click( function() {
        var dataArray = JSON.parse($( this ).attr("data"));
        var month = dataArray[0];
        var week = dataArray[1];
        console.log(dataArray, month, week);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a class="quickie" data='["jan",1]'>week 1</a>
<a class="quickie" data='["jan",2]'>week 2</a>
<a class="quickie" data='["jan",3]'>week 3</a>

1 Comment

I'll try swapping the quotes, but the closing }); was my fault in typin gthe question, it is not that way in the code... thx
0

Excellent - the solution was a combination of two suggestions...

1 - had to change my "quoting structure" - it's a little frustrating, but oh well

2 - the JSON.parse then works with the updated quotes

thank you all.

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.