JavaScript - Concatenate a value within variable - javascript

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)

Related

How to slice part of string from the middle to the end using javascript

I have string like this:
река Марица - гр. Първомай + Maritsa - 132543 - HPoint-Water level
I want to get only Maritsa - 132543 - HPoint-Water level with javascript.
For now I get the string and console log all string, but I need to cut the cyrillic string and - before Maritsa.
The problem is that the loaded names are always different. Is there a way to delete everything before the + including it and show everything after the +
You could replace the unwanted part.
const s = 'река Марица - гр. Първомай + Maritsa - 132543 - HPoint-Water level';
console.log(s.replace(/.*\+\s?/, ''));
You can combine String.prototype.indexOf (which returns the first occurence of a char) and Array.prototype.slice (to return the string only after a certain index, here after the + and the space after it) :
const name = 'река Марица - гр. Първомай + Maritsa - 132543 - HPoint-Water level'
const cutString = string => string.slice(string.indexOf('+') + 2)
// add 2 to also remove the + and the space after it ^
console.log(`"${ cutString(name) }"`)
Hope it helped you !

How to mix string with variables in an input needing mutiple values in js? (In this case, the coord attribute of the area tag)

So what the problem I have been facing so far is that I am currently trying to use javascript so that the coords attribute of my area tag is set according to some variables(what it is doesn't really matter). However, the coords require 4 inputs (x1,y1,x2,y2) and I have some values that I am not going to use the variable for. Which means that I have to mix string and variables in the input which I have no idea on how to do.
To give you a better sense on what I am doing, here is a summary:
var p = some random value
var q = some random value
var a1 = document.getElementById(areaID);
a1.setAttribute("coords", "0,0, p,q")
Of course this didn't work as the "" made it think that p,q are strings instead of variables. So I tried some other where it all failed(some desperate attempts).
a1.setAttribute("coords", 0,0,p,q);
a1.setAttribute("coords", "0,0," + p + "," + q);
document.getElementById(areaID).coords = 0,0, p,q;
const list = ["0","0", p,q];
a1.setAttribute("coords", list);
So does anyone know how could I possibly do this?
The second option you tried should work. In your example, is areaID a variable or the actual id of the element? If it is the latter, then you need to put it in quotes: document.getElementById("areaID")
var p = "val1"
var q = 23
var a1 = document.getElementById("areaID");
a1.setAttribute("coords", "0,0," + p + "," + q)
console.log(a1);
<div id="areaID"></div>

Jquery convert object to string and slice

Via ajax-based script i get object:
for example:
item.TYP_PCON_START
which value is, for example 201212...
When i try to slice him, i get oject error...
How could i slice this object so, that for example i get 2012, or better set two last numbers on furst place and add dot, like:
12.2012
How could i do this? (i append this text as value of select list)
You need to slice the string property, not the object itself:
item.TYP_PCON_START.slice(-2) + '.' + item.TYP_PCON_START.slice(0, 4);
> '12.2012'
http://jsfiddle.net/4Hdme/
edit: In the case that your property is a number, you must convert it to a string before attempting to slice it:
var propertyAsString = item.TYP_PCON_START.toString();
propertyAsString.slice(-2) + '.' + propertyAsString.slice(0, 4);
> '12.2012'
http://jsfiddle.net/4Hdme/1/
var a = item.TYP_PCON_START,
a = a+"",
a = a.split("");
a.splice(2,0,".");
a = a.join("");
a = parseFloat(a);
console.log(a);

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!

Why does eval() give undefined value in 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).

Categories