Reputation: 651
I have a simple concept here.I am getting some values on click of first button in my console,which comes as increment index++. I have already combined the value and put into an array onclick of second button, now the output is coming as
["$('#chart1')", "$('#chart2')", "$('#chart3')"],
But the only thing I need to fix is to remove the double cotes that surrounds each value inside array.I need the output like this
[ $('#chart1'), $('#chart2'),$('#chart3')]
Here is code below.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div><button id="button1" class="button1">submit1</button> </div>
<div><button id="garray" class="button1">genarete-array</button> </div>
var index = 0;
var id = [];
$('#button1').on('click', function() {
index++;
id.push(`$(\'#chart${index}\')`)
console.log('chart' + index);
});
$('#garray').on('click', function() {
console.log(id);
//output [ $('#chart1'), $('#chart2'),$('#chart3')]
})
Upvotes: 1
Views: 117
Reputation: 1805
You can not store it without the double quotes but you are able to replace it just like below.
var index = 0;
var id = [];
$('#button1').on('click', function() {
index++;
var temp="$('#chart"+index+"')";
id.push(temp);
console.log('chart' + index);
});
$('#garray').on('click', function() {
var string = JSON.stringify(id);
console.log(string.replace (/"/g,''));
//output [ $('#chart1'), $('#chart2'),$('#chart3')]
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div><button id="button1" class="button1">submit1</button> </div>
<div><button id="garray" class="button1">genarete-array</button> </div>
One more thing is that if you wish to remove quotes from other than string type you may use map function.
stringvaribale.map(parseFloat), stringvaribale.map(Number),etc.
Upvotes: 1
Reputation: 29
Once you push it to an array it normally is string according to your data, so for that situation maybe you should convert it to JSON.stringify
then try to use a regular expression data.replace(/"/g,"")
to get rid of the cotes
Upvotes: 0
Reputation: 7121
Try this code:
id.push($(`#chart${index}`));
You can test it here: https://jsfiddle.net/alonshmiel/x25btyn1/5/
Upvotes: 1