Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The only possible way to achieve what you need is through the evil <code>eval()</code>, as it is not possible to access local variables through their name as strings. (Globals are possible using <code>window["variableNameAsString"]</code>.)</p> <p>The solution snippet presented below has the following effect:</p> <blockquote> <p>Returns <strong><code>true</code></strong> if <strong><code>varName</code></strong> was declared and initialized (even if with <strong><code>null</code></strong>) and is readable from the current scope. Otherwise returns <strong><code>false</code></strong>.</p> </blockquote> <p><strong><a href="http://jsfiddle.net/acdcjunior/8kHNu/" rel="nofollow">DEMO jsFiddle here.</a></strong></p> <h3>Source:</h3> <pre><code>function removeIdOfAllElementsWithId(id) { var element, elementsFound = []; while ((element = document.getElementById(id)) !== null) { element.removeAttribute('id') elementsFound.push(element); } return elementsFound; } function assignIdToElements(elements, id) { for (var i = 0, n = elements.length; i &lt; n; i++) { elements[i].id = id; } } var isDefinedEval = '(' + function (isDefinedEvalVarname) { var isDefinedEvalResult; var isDefinedEvalElementsFound = removeIdOfAllElementsWithId(isDefinedEvalVarname); try { isDefinedEvalResult = eval('typeof '+isDefinedEvalVarname+' !== "undefined"'); } catch (e) { isDefinedEvalResult = false; } assignIdToElements(isDefinedEvalElementsFound, isDefinedEvalVarname); return isDefinedEvalResult; } + ')'; </code></pre> <h1>Usage:</h1> <p>To test if a variable with name <code>variableName</code> is defined:</p> <pre><code>eval(isDefinedEval + '("' + 'variableName' + '")') === true </code></pre> <p>To check if it is not defined:</p> <pre><code>eval(isDefinedEval + '("' + 'variableName' + '")') === false </code></pre> <p>In the fiddle you'll find lots of unit tests demonstrating and verifying the behavior.</p> <h3>Tests:</h3> <ul> <li>Several tests are included in the fiddle;</li> <li>This snippet was tested in IE7, IE8 and IE9 plus latests Chrome and Firefox.</li> </ul> <h3>Explanation:</h3> <p>The function must be used through <code>eval()</code> because it needs to have access to the variables locally declared.</p> <p>To access such variables, a function must be declared in the same scope. That's what <code>eval()</code> does there: It declares the function in the local scope and then calls it with <code>varName</code> as argument.</p> <p>Aside that, the function basically: - Removes the ID attribute of every element that has an ID === <code>varName</code>; - Checks if the variable is <code>undefined</code>; - And reassign the ID of those elements it removed the attribute.</p> <p><sup>Note: this answer was heavily edited, some coments may not be still appliable.</sup></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload