...">

Get the item id and set it as a 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 easily get id: currForm.attr('id'); , but can I set the value of this id as a variable.

Something like php variable variables:

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

Change I do not want to change the identifier of the element. I want to get this identifier and use it as a name for a javascript variable. For example, the element I above is in a form that has ID = 'example_id'. currForm.attr('id') will give me example_id , and I want to set the example_id variable and assign it a value.

var example_id = 'some value';

+6
source share
6 answers

Here are 3 options for you.

eval (not recommended)

You can use the Javascript eval function to achieve what you want. But keep in mind that this is not recommended (I emphasized this twice, I hope you understand!).

Do not use eval unnecessarily!

eval () is a dangerous function that executes the code passed to it with the privileges of the caller. If you run eval () with a line that can be affected by the malicious side, you can run malicious code on the user's computer with the permissions of your web page / extension. More importantly, third-party code can see the area in which eval () was called, which can lead to possible attacks so that such a function is not susceptible.

It will be used as follows:

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

Window object designation (better than eval , still not recommended)

This method consists of using the object notation provided by JavaScript in the global window object. This pollutes the global area of ​​the window and can be undone by other JS files, which is bad. If you want to know more about this subject, this is a good question: Saving a variable in a JavaScript window object is the right way to use this object? .

To use this method, you will do this:

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

Use of the facility (recommended)

The best solution would be to create your own variable scope. For example, you can create a variable in the global scope and assign an object to it. Then you can use the object notation to create dynamic variables. It works the same way a 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 } 
+6
source

You can do this using a javascript object:

 var currForm = $(this).parents('form');//form id="hello" var obj = {}; obj[currForm.attr('id')] = 30; console.log(obj.hello) 
+2
source

You can use objects to store your variables in:

 var variables = {}; 

add a type variable:

 variables[name] = value; 

and to access the value:

 variables[name] 

Take a look here: jsFiddle

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

+1
source

Your best option for archiving what you really want is to use eval (), for example:

 eval("var " + currForm.attr('id'); 

Check out this link β†’

Dynamic variables in javascript

0
source

Try one of the following:

 $(this).attr(example_id, "value") 

or

 window[ currForm.attr(id)] = 12; 

or

 window[ this.id ] = 12; 
0
source

Use eval (but not recommended):

 var id = 'that_id'; eval(id+" = 'other id';"); alert(that_id); // results: 'other id' 
-1
source

Source: https://habr.com/ru/post/975784/


All Articles