5

I have a number of javascript variables on my page:

var opts_???? = ...
var opts_???? = ...
var opts_???? = ...

The ???? is assigned a random number by the content management system, so I don't know the full name of the variable.

I'm looking for any undefined ones.

Is there a way in jQuery to loop through all variables that start with opts_ so that I can test them for being undefined?

The variables are all global, if that helps.

If not in jQuery, I'll settle for regular javascript.

3
  • Does the variable name correspond to a name, class, or id attribute of an html element on the page? Commented Jun 7, 2016 at 14:21
  • No these are just variables Commented Jun 7, 2016 at 14:23
  • what do you mean by undefined? Commented Jun 7, 2016 at 14:30

4 Answers 4

7

This is only possible if the variables have all been declared at global scope (and therefore also available as properties of the global window object).

Since yours are, you can use Object.keys(window) to obtain the names of all such properties, and then use either $.each or Array.prototype.forEach to test each of them in turn.

var opts = Object.keys(window).filter(function(n) {
    return n.substring(0, 5) === 'opts_';
});

var opts_undefined = opts.filter(function(n) {
    return window[n] === undefined;
});

[written as two calls for clarity over efficiency]

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

Comments

6

If the variables are in the global scope (that is, not created within a function) they should also be available as properties on the window object. In other words, the variable opts_1234 can also be accessed as window.opts_1234 or window['opts_1234'].

The easiest way to grab all the variables would be:

var variables = Object.keys(window).filter(function(prop) {
    return prop.substring(0, 5) === 'opts_';
});

Now variables contains an array of names, like ['opts_1', 'opts_666']

You can also extend that filter to only include those variables that aren't undefined:

var variables = Object.keys(window).filter(function(prop) {
    return prop.substring(0, 5) === 'opts_' && window[prop] !== undefined;
});

4 Comments

.indexOf is (IMHO) the wrong function for this job.
I considered using a regex, but I'm not sure it would make a big difference. What do you suggest?
well, in my answer I've used .substring(), since I explicitly only want to test the first five characters. The problem with .indexOf is that it'll scan the entire string unnecessarily.
Good point, I'll update my answer
1

var opts_1 = 'test';
var opts_2 = 'test1';
var opts_3 = 'test2';
var opts_4 = undefined;

var vars = Object.keys(window).filter(function(key){
   return (key.indexOf("opts_")!=-1 && window[key] == undefined) 
});
console.log(vars);

Comments

0

Use:

var marker='opts_';
var results=$('selector').find(marker);

Hope This will help you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.