0

The following is my code, i intend to use it as a bookmarklet.

javascript: (function () {   
    var jsCode = document.createElement('script');   
    jsCode.setAttribute('src', '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js');                    
    document.body.appendChild(jsCode);
    console.log("Everything is fine upto here!");
    data = [["#txtApplcntName","test"],["#txtApplcntAdd","test"]];
    console.log(data);
    var obj = $.parseJSON(data);
    for (var i = obj.length - 1; i >= 0; i--) {
        var current = obj[i];
      console.log(current);
        $(current[0]).val(current[1]);
    };

 })();

Problems start when the actions in the for loop never take place. It gets even weirder when i can successfully log the variable obj and it logs, but when i do obj.length a null is encountered?

I am experimenting on Google chrome

2 Answers2

0

var obj = $.parseJSON(data)

obj get a null ."data" can't convert to json object.

Tin
  • 24
  • 1
0

Try this:

data = '[["#txtApplcntName","test"],["#txtApplcntAdd","test"]]';

Method JSON.parse(str, reviver) reads a string. What you're trying to do is treat an ordinary array as JSON.

Artur Udod
  • 4,465
  • 1
  • 29
  • 58