2

I want to get all the variables which are set on DOM before with a single function. I am not sure it is possible in javascript or not.

var a= 1;
var b=2;
var =3;

var all_data= get_all_vars();

function get_all_vars(){
//this function should return 
// return array('a'=>1, 'b'=>2, 'c'=>3);
}

Please suggest me the answer. Either it is possible or not. If it is possible how to do it? Thanks In advance.

1

2 Answers 2

1

You can use

for(i in window) console.log(i);

or

Object.keys(window)

but it'll return every global variable. There's no way to do exactly what you want, unfortunately.

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

1 Comment

@thefourtheye Impossible to say, it depends on the context of the code in the question. If it was literally just that inside of a <script> tag then yes, they'd be global (even though they used the var keyword).
0
//el is your DOM object
 function get_all_vars(el){
      var ret = {};
      for(p in el){
        if(el.hasOwnProperty(p)){
           ret[p]=el[p]; 
        }
      }
      return ret; 
    }

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.