What is wrong with my code. How do i pass attrfull to the inside. The way i have it done, if i run the function editsubmit($selected, size), $selected is inserted properly but i'm getting attrfull instead of size.
function editsubmit(attr, attrfull) {
if (attr.length) {
attr.val().length ? $selectedinput.attr({
attrfull: attr.val()
}) : $selectedinput.removeAttr(attrfull);
}
}
$selected is a variable and attrfull i a string. Do i need double qoutes around the string when i run the function like editsubmit($selected,'size').
Try
function editsubmit(attr, attrfull) {
if (attr.length) {
attr.val().length ? $selectedinput.attr(attrfull, attr.val()) : $selectedinput.removeAttr(attrfull);
}
}
Yes, you do need it to be a string (in double quotes), or else it will think you're trying to pass a variable reference.
The problem is this:
{attrfull: attr.val()}
I think you want it to be
{size: (whatever attr.val() is)}
So:
function editsubmit(attr, attrfull) {
if (attr.length) {
if (attr.val().length) {
var myObj = {};
myObj[attrfull] = attr.val();
$selectedinput.attr(myObj);
} else {
$selectedinput.removeAttr(attrfull);
}
}
}
Related
I'm write Cypress script is say js, but I have some issue about variable checking
the code is like this
var baseUrl = Cypress.config('baseUrl');
// baseUrl can return http://developmachine1.localhost/test
I want to do simple condition
if (baseUrl.contains('localhost'))
{
do something ...
}
else
{
do something ..
}
but it will throw TypeError : baseUrl.contains is not a function
can I know to do it ?
Thank you
The JS function for checking if a string contains another string is includes.
if (baseUrl.includes('localhost')) {
// code
} ...
This is some JS code
var methodArr = ['firstFunc','secondFunc','thirdFunc'];
for(var i in methodArr)
{
window[methodName] = function()
{
console.log(methodName);
}
}
My problem is that how to get the name of a function in JS.
In JS, use this.callee.name.toString() can get the function name. But in this situation, it is a null value. How can i get the 'funName' string?
Sorry, I didn't make it clear.
I want to create functions in a for loop, all these functions has almost the same implementation which need its name. But others can call these functions use different name.I want to know what methodName function is called.
it seems a scope problem.
Try this:
var methodArr = ['firstFunc','secondFunc','thirdFunc'];
for(var i in methodArr) {
var methodName = methodArr[i]; // <---- this line missed in your code?
window[methodName] = (function(methodName) {
return function() {
console.log(methodName);
}
})(methodName);
}
window['secondFunc'](); // output: secondFunc
In javascript, I have an object that I am trying to pass but not working:
var inf1= { ID: "34343434" };
What I like to do in javascript is to pass this object to a function .
I am not sure how to do this.
I have the calling function as such
function getinf(inf1)
{
var samp = JSON.parse(inf1);
alert(samp.ID);
}
You use JSON.parse() on a JSON formatted string. You don't use it on a javascript object. JSON is a text format.
Your inf1 variable is already a javascript object so there is no need to parse it.
This should work just fine:
var inf1= { ID: "34343434" };
function getinf(item)
{
alert(item.ID);
}
getinf(inf1);
Careful, a few things:
JSON.parse() is for strings
And to pass the value you could do:
var inf1= { ID: "34343434" };
function getinf(obj)
{
//code using obj...
//you can acess obj.ID if you want
}
getInf(inf1); //or any obj you want
or
var inf1= { ID: "34343434" };
function getinf()
{
//code using inf1...
}
I want execute JavaScript function which the name is coming as a string dynamically.
I don't need to pass any parameters while executing the function.
Please can any one guide me how to achieve this?
one simple way
eval("SomeFunction()");
or
var funcName = "SomeFunction";
var func == window[funcName];
func();
dangerous but you could use eval(method_name+"()")
are you talking about ´eval()´??
var foo = "alert('bar')";
eval(foo);
Hope this helps;
function a() {
console.log('yeah!');
}
var funcName = 'a'; // get function name
this[funcName]();
If the function is global, you should do window[funcName]() in browser.
Using eval is the worst way imaginable. Avoid that at all costs.
You can use window[functionname]() like this:
function myfunction() {
alert('test');
}
var functionname = 'myfunction';
window[functionname]();
This way you can optionally add arguments as well
Perhaps a safer way is to do something like this (pseudo code only here):
function executer(functionName)
{
if (functionName === "blammo")
{
blammo();
}
else if (functionName === "kapow")
{
kapow();
}
else // unrecognized function name
{
// do something.
}
You might use a switch statement for this (it seems like a better construct):
switch (functionName)
{
case "blammo":
blammo();
break;
case "kapow":
kapow();
break;
default:
// unrecognized function name.
}
You can optimize this by creating an array of function names, searching the array for the desired function name, then executing the value in the array.
so I have a button with this event:
onmousedown="hideElements('\x22cartview\x22,\x22other\x22')"
and then this function hideElements:
function hideElements(what)
{
var whichElements=[what];
alert(whichElements[0]);
}
I want it to alert "cartview" but it alerts
"cartview","other"
I am aware of the arguments object but in this case I don't know how to use it to access the individual strings that are comma separated. Probably there is an easy solution but I am kind of new to this. Thanks!
onmousedown="hideElements([ 'cartview', 'other' ])"
and then:
function hideElements(what) {
alert(what[0]);
}
It looks like the real problem is that you're passing an string, not an array. So you'd do something like:
function hideElements(/* String */ what) {
alert(what.split(',')[0]);
}
or with an array:
function hideElements(/* Array<String> */ what) {
alert(what[0]);
}
or passing multiple strings directly into the function:
function hideElements(/* String */ what) {
alert(arguments[0]);
}