Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element's ID and set it as variable

I have a button:

<button class="btn btn-info continue">
    <i class="ace-icon fa fa-check bigger-110"></i>
    Continue                                        
</button>

Onclick:

$(".continue").click(function(e) {
    currForm = $(this).parents('form');
    e.preventDefault();
});

I can get the id pretty easily: currForm.attr('id');, but can I set the value of this id as a variable.

Something like php's variable variables:

$a = 'hello';
$$a = 'world';
echo $hello;

Edit: I don't want to change element's id. I want to get this ID and use it as a name for a javascript variable. For example, the element I provided above is in a form that has ID='example_id'. currForm.attr('id') will give me example_id and I want to set a variable example_id and assign a value to it.

var example_id = 'some value';

like image 759
Ivanka Todorova Avatar asked Sep 24 '14 14:09

Ivanka Todorova


People also ask

Can an id be a variable?

An ID variable is a variable that identifies each entity in a dataset (person, household, etc) with a distinct value. This article lists five properties of ID variables that researchers should keep in mind when creating, collecting, and merging data.

How do you find the value of an element by id?

The getElementById() method returns the elements that have given an ID which is passed to the function. This function is a widely used HTML DOM method in web designing to change the value of any particular element or get a particular element. If the passed ID to the function does not exist then it returns null.

How do I select all elements by id?

Use the document. querySelectorAll() method to get all elements whose id starts with a specific string, e.g. document. querySelectorAll('[id^="box"]') . The method returns a NodeList containing all the elements that match the provided selector.

How to get element by id name class and tag value?

JavaScript Get Element By id, name, class, tag value 1: JavaScript getElementById () method The JavaScript getElementById () is a dom method to allows you to select an... 2: JavaScript getElementsByClassName () method The JavaScript getElementByClassName () is a dom method to allows you to... 3: ...

How to select an element by its ID in JavaScript?

The JavaScript getElementById() is a dom method to allows you to select an element by its id. The following syntax represents the getElementById()method: let element = document.getElementById(id); Note that, the id is case-sensitive.

What is the difference between getElementById () and getan ID?

An ID should be unique within a page. However, if more than one element with the specified ID exists, the getElementById () method returns the first element in the source code. The numbers in the table specifies the first browser version that fully supports the method.

What is getElementById () method in JavaScript?

The JavaScript getElementById () is a dom method to allows you to select an element by its id. The following syntax represents the getElementById () method: 1. let element = document.getElementById (id); Note that, the id is case-sensitive. For example, the 'myId' and 'MyId' are totally different ids. This method returns an Element object that ...


3 Answers

Here's 3 options for you.

eval (not recommended)

You can use the Javascript function eval to achieve what you want. But be aware, this is not recommended (I emphasized it twice, hope you understood!).

Don't use eval needlessly!

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.

It would be used like that :

eval('var ' + varName + ' = "something";');

Window object notation (Better than eval, still not really recommended)

This method consists of using the object notation provided by JavaScript on the global window object. This is polluting the global window scope and can be overridden by other JS files, which is bad. If you want to know more about that subject, this is a good question: Storing a variable in the JavaScript 'window' object is a proper way to use that object?.

To use this technic, you would do something like:

window[varName] = something;
alert(window[varName]);

Using an object (recommended)

The best solution would be to create your own variable scope. For instance, you could create on the global scope a variable and assign an object to it. You can then use the object notation to create your dynamic variables. It works the same way as the window does :

var globalScope = {};

function awesome(){
    var localScope = {};
    globalScope[varName] = 'something';
    localScope[varName] = 'else';

    notSoAwesome();
}

function notSoAwesome(){
    alert(globalScope[varName]); // 'something';
    alert(localScope[varName]); // undefined
}
like image 146
Karl-André Gagnon Avatar answered Oct 21 '22 22:10

Karl-André Gagnon


You can do it using javascript object:

var currForm = $(this).parents('form');//form id="hello"

var obj = {};

obj[currForm.attr('id')] = 30;

console.log(obj.hello)
like image 31
Ragnar Avatar answered Oct 21 '22 21:10

Ragnar


You can use and object to store your variables in:

var variables = {};

to add a variable just type:

variables[name] = value;

and to access the value:

variables[name] 

Check it out here: jsFiddle

Button 2 reads the value of variables[formid] and button 1 sets formid to submitted

like image 1
Johan Karlsson Avatar answered Oct 21 '22 22:10

Johan Karlsson