Why does eval() give undefined value in Javascript? - javascript

I am working in JavaScript coding. I have created a text area with name OQ_0 and value "0". When i use eval() method for that field in JavaScript it is giving the value undefined. The below are the part of JavaScript code
var tempOpenQtyStr = "document.InitiateReturnsForm.OQ" + "_" + 0;
var tempOpenxQtyStr = eval(tempOpenQtyStr).value;
alert('Manuals =' + document.InitiateReturnsForm.OQ_0.value);
alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));
alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);
Output:
Manuals = 0
eval(tempOpenxQtyStr ) = 0 --- Here it is suppose to show "[object]"
eval(tempOpenxQtyStr).value = undefined.
Kindly help me out what is change to do. Thanks in advance.

Why not just use document.InitiateReturnsForm["OQ_" + 0].value?

Try
alert('eval(tempOpenxQtyStr ) = ' + eval(tempOpenQtyStr));
alert('eval(tempOpenxQtyStr).value = ' + eval(tempOpenQtyStr).value);
In the second and third alert you are evaluating the second variable which stores the value of the first evaluated object. That's why the error occurs.

alert('eval(tempOpenxQtyStr ) =' + eval(tempOpenxQtyStr));
Since you put a string, not an object, inside tempOpenxQtyStr, it evaluates that string and returns 0.
alert('eval(tempOpenxQtyStr).value =' + eval(tempOpenxQtyStr).value);
Here you're using a method on a variable that contains a string. That doesn't work. It doesn't have that method, that's why it returns undefinied.
You might want to try doing eval(tempOpenxQtyStr.value) instead of eval(tempOpenxQtyStr).value since the last one does basically nothing, just evaluating an object and then fetching the objects value (it doesn't eval the value itself).

Related

JavaScript - Concatenate a value within variable

I know that this is somewhat, very basic question, but..
I have to create a cypress element, for which I have to concatenate a value between variables.
Like I have this value
const value = "100 - 299"
and I have to print a value within a string, i.e.
cy.get('[data-value="100 - 299"]')
I am trying everything but unable to do so.
Can someone help me on this?
I would consider template literals - also called backticks
cy.get(`[data-value="${value}"]`)
You could consider concatenation.
const value = "100 - 299"
cy.get('[data-value="' + value + '"]')
Or template literals ( check compatability: https://caniuse.com/template-literals )
cy.get(`[data-value="${value}"]`)
Or pre-concatenation:
const value = "100 - 299"
const fullstring = '[data-value="' + value + '"]'
cy.get(fullstring)

JS: how to filter table using variable

I have a function that I need to use for filtering table rows:
setFilterString("Filter");
But I have a problem. I can set it to
setFilterString("OrderID = 5");
and it will filter out row where OrderID is equal to 5 but if i try using a variable that has a value taken before like this
setFilterString("OrderID = vOrderID");
I get error "Invalid column name 'vOrderID'." (as vOrderID is variable and not a column, I guess)
I have seen somewhere in filter section inputting something like this ("OrderID = '" & vOrderID & "'") but it doesn't have any result at all for me. Doesn't even throw any error in the console.
JavaScript assumes you are just passing a string to the function. If you want to use the variable, you should try this:
setFilterString("OrderID = '" + vOrderID + "'"); // Results in OrderID = '5'
or
setFilterString("OrderID = " + vOrderID); // Results in OrderID = 5
depending on the body of your function.
Use + instead of &: setFilterString("OrderID = " + vOrderID) should work.
Use "+" for merge strings:
setFilterString("OrderID = " + vOrderID)
You can also try to use ${idvOrderID} inside string:
setFilterString("OrderID = ${vOrderID}")
Or:
setFilterString(sprintf("OrderID = %s", vOrderID))
Remember about difference between ' and "

Printing expression values inside comments in Jade template

Need to print the value of a parameter inside a Jade template comment. The value is passed-in correctly; the following works as expected:
if type !== "surveyFree"
ul.pollOptions
for answer, idx in answers
li
+printAnswer(answer, idx)
else
textarea(cols="50", rows="4", placeholder="Type in your answer")
However, all attempts to print out the value of the type variable inside a comment have failed. I've tried the following forms:
// "type = " + type
// #{"type = " + type}
// span= #{"type = " + type}
// span= type = #{type}
and many others, but nothing works (everything gets printed exactly as I typed it in). I'd like the output:
<!-- type = challenge -->
or
<!-- type = survey -->
All help appreciated!
Maybe this way will be good for you:
!='<!-- COMMENT VARAIBLE: ' + variable + '-->'

jQuery - Addition

I have an issue in doing a simple addition and save the value in a variable.
Basically I have the following code:
var accsen;
var lowsev = parseInt(accsen);
var hisev = parseInt(accsen) + parseInt(0.65);
console.log('Lowsev: ' + lowsev);
console.log('Hisev: ' + hisev + ' Type: ' + typeof(hisev));
console.log('Accsen: ' + accsen);
The variable accsen is being given a value from the database. Lowsev is being assigned the same value as accsen,while hisev is being assigned the value of accsen + 0.65.
However the issue I am having is that both lowsev and hisev are remaining 0. On doing console.log I get these values:
Lowsev: 0
Hisev: 0 Type: undefined
Accsen: 0.75
Can anyone tell me what I am doing wrong in the addition? Am I using the correct operators?
Sounds like you want to use parseFloat instead of parseInt.
"Lowsev is being assigned the same value as accsen" It's not, you're rounding it to an integer.
But parseInt() doesn't round properly. 0.75 comes out as 0, so it's working. Assuming you actually want to round these values try
var accsen;
var lowsev = Math.round(accsen);
var hisev = Math.round(accsen) + Math.round(0.65);
EDIT given the response
Your JS is treating accsen as a string, you need to convert to a number
var accsen = '0.75'; // as other people have noted this val in your code is missing.
var lowsev = parseFloat(accsen);
var hisev = parseFloat(accsen) + 0.65;

Unable to concanate String+Variable+String in Javascript

I am trying to dynamically have my javascript look for an element ID in the DOM.
I am currently using this
var string = "retail";
document.getElementById('markup_'+string+'_percentage').value=z.toFixed(2)+"%";
Where the variable "string" has a value like "retail"
This I thought would give a concatenated string of "markup_retail_percentage".
However it actually gives this as an error message:
document.getElementById("markup_"+string+"_percentage") is null
I have tried also using the "." and " * " operators.
One of my html elements
<input type="text" id="markup_retail_percentage" size="5" name="markup_retail_percentage" value="" readonly />
SOLUTION!!!!
//using a new variable name to be passed to function
function percentage(elementid)
{
elementid = "markup_" + elementid;
elementid = elementid + "_percentage";
document.getElementById(elementid).value = "a value";
}
I see two^w three^w four possibilities:
string doesn't contain what you think it does
you should have an underscore before percentage.
the specified element really doesn't exist! (thanks #jAndy)
it does, but the DOM isn't ready yet (thanks #Yoshi)
It looks like you are missing an underscore ahead of percentage
document.getElementById('markup_'+ string +'percentage')
I think you want
document.getElementById('markup_'+ string +'_percentage')
If you run this code before the Document has finished loading, your markup will not have been fully parsed and the Element with that id will not be accessible using DOM methods.
Solution:
function percentage(elementid)
{
elementid = "markup_" + elementid;
elementid = elementid + "_percentage";
document.getElementById(elementid).value = "a value";
}
I dont know why its working:
-Could be concatenation problem , not allowed to use multiple " + " operators?
-Change variable name ?
But it is working now, so thanks to all!

Categories