I am making a div and in its onclick function I am calling function Remove() to which I am passing its id and name. But when I use name in my remove function, an undefined error is thrown. For example, I call Remove(1,xyz) and then in the Remove function I am unable to access xyz; it's showing me xyz is not defined.
Here is my jQuery code which calls the Remove function:
$("#h2-"+id).append("<div id = 'c2-"+id+"' onclick = 'Remove("+id+","+name+")' class='clear_btn1'> </div>");
and here is my Remove function:
function Remove(i, name){
alert("I am deleting " +name);
var sender = "<?php echo $user_check?>";
var receiver = name;
console.log("Name is " +sender);
console.log("receiver is " +receiver);
}
The value of i is coming perfectly fine but I cannot access name in my function.
Change your code as below
$("#h2-"+id).append("<div id = 'c2-"+id+"' onclick = \"Remove("+id+",'"+name+"')\" class='clear_btn1'>sdfsdf sdfsdf</div>");
What is saved in onclick is actually Remove(id,name). This looks okay at first look, but...
Say id=10 and name="Mark".
You would be calling Remove(10,Mark), which is not what you want. Mark would be treated as a variable. You therefore need to put additional quotes enclosing name to treat it as a string.
You should, of course, escape the additional quotes you would add.
You need to call Remove(10,"Mark"). Notice the quotes.
Related
I want to create link that send to function:
tdLink2.innerText="Delete";
tdLink2.href="javascript:deleteDepartment(id)"
but the "id" parameter was not sent.
How can I insert the parameter?
ID is not parsed in your string
EITHER (Don't forget the extra quotes if ID is a string
tdLink2.href="javascript:deleteDepartment('"+id+"')"
alternative with template literals
tdLink2.href=`javascript:deleteDepartment('${id}')`;
I would personally keep DELETE far away from a href
This is better
tdLink2.href="#"
tdLink2.addEventListener("click",function(e){
e.preventDefault(); /stop the link
deleteDepartment(id); // id is some global variable
})
EVEN better is to do
tdLink2.href="#";
td.dataset.id = id; // assign to a data attribute
tdLink2.addEventListener("click",function(e){
e.preventDefault(); // stop the link
deleteDepartment(this.dataset.id); // pass the data attribute
})
If id is already a defined variable then you can do like this:
tdLink2.href=`javascript:deleteDepartment(${id})`
You can do this if it is of type string.
Otherwise you can go for this:
function f(){
deleteDepartment(id)
}
tdLink2.href='javascript:f()'
Just assign function result to property
tdLink2.href = deleteDepartment(id);
I wanted to get all JavaScript Variables. So I followed instructions in this topic and it worked smoothly.
Get all Javascript Variables?
Now I also want to get all strings, that are not declared as variable. For example in below code when I iterate through this I get the value of variable hello in output. However, since "Passing My Message" string is not declared as variable, I don't get this string in output.
<script>
function MyFunction(msg){
alert('Message Passed : '+msg)
}
var hello = "AAA";
MyFunction("Passing My Message");
for (i in this){
console.log(i + " : " + eval(i));
}
</script>
Now my question is, is there any way I can get the Passing My Message string in output.
You won't. When you call the function MyFunction it creates the variable msg. When the function finishes the variable msg is removed.
If you checked inside the MyFunction function then you would see the msg variable but you won't see it at any other time.
When I want to redirect, the variable where is always udefined. But, for example, I want put that variable in alert(); it shows correct number.
code
var where = msg.txt;
window.location = "/page.php?id=".where; //this redirects to /page.php?id=undefined
alert(where); //it show correct number
It should be:
window.location = "/page.php?id=" + where;
You have:
"/page.php?id=".where;
Which tries to retrieve a where property of a string, and such has not been defined.
In JavaScript, . is used for property access, not for string concatenation like in PHP.
Use + instead:
window.location = "/page.php?id=" + where;
I have a string like this:
var b = "<button type='button' onclick='javascript:"+f+"(\""+ encodedString+"\");'> hi</button>"
//encodedString = "a " <hr> <br>"
and after I do something like:
$('li').append(b);
after this the encodedString becomes decoded and my onclick doesn't work because of the " becoming "
Instead of directly setting the click event through html, bind an event listener. eval(f) allows you to refer to functions by a string. If possible, I recommend passing a direct reference to the function.
If the function is defined in the global scope, use window[f]. Note: This won't work if f looks like Math.round, because window["Math.round"] != window["Math"]["round"].
//f is a string which contains a single function name.
var b = $('<button>').click(function(){
eval(f)(encodedString);
});
$('li').append(b);
If f is a dynamic variable, which changes (eg, in a loop), wrap the code in an anonymous function:
(function(f){
var b = $('<button>').click(function(){
f(encodedString);
});
$('li').append(b);
})(eval(f));
I am using an onsubmit variable to ensure that the user really means to delete something, however as soon as I put a value in the parenthesis inside the onsubmit it no longer calls the confirm box.
Code:
onClick="confirmSubmit(abc)"
Doesn't work but the following:
onClick="confirmSubmit()"
Does work
Function:
function confirmSubmit(category)
{
var category = category;
var agree=confirm("Are you sure you wish to DELETE" + category + " and all of its subcategories and photos?");
if (agree)
return true ;
else
return false ;
}
you need quotes around your abc:
onclick="confirmSubmit('abc')"
Without them you are trying to pass a variable, abc, which doesn't exist and triggers an error
onClick="confirmSubmit(abc)" is trying to pass the variable abc, if you intend to pass a string with the value "abc" then do this:
onClick="confirmSubmit('abc')"
function confirmSubmit(category)
{ var category = category;
And you've declared "category" twice! Once in the function header and then as a function variable in the next line! What for?
You're try to pass the variable abc (which does not exist) to the function.
Do:
onclick="return confirmSubmit('abc');"