-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.

j-p
  • 3,698
  • 9
  • 50
  • 93
  • 1
    It's `JSON.parse()`. Here, you are converting a String to... a String. – blex Jun 28 '15 at 19:00
  • possible duplicate of [Difference between JSON.stringify and JSON.parse](http://stackoverflow.com/questions/17785592/difference-between-json-stringify-and-json-parse) – blex Jun 28 '15 at 19:05

3 Answers3

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' ) );
Austin Brunkhorst
  • 20,704
  • 6
  • 47
  • 61
  • SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON datavar dataArray = JSON.parse( $( this ).attr( 'data' ) ); – j-p Jun 28 '15 at 19:30
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>
guest271314
  • 1
  • 15
  • 104
  • 177
  • 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 – j-p Jun 28 '15 at 19:33
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.

Community
  • 1
  • 1
j-p
  • 3,698
  • 9
  • 50
  • 93