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');"
Related
I have a script that checks whether the value of a selected element equals the span id. Everything works fine except for the variable assignment.
To be more precise: The function itself works (if i alert object.id it is displayed right) but the variable assignment doesn't. If I try to alert the variable, it says it's undefined. I'm sure it's some minor mistake and it would be very nice if someone could point it out:
var spanId = (function getId(object)
{
return object.id;
//alert(object.id);
})();
Setting the variable will not update that variable when you call it again and again. You need to restructure your code in order to update that variable.
var spanId;
function getId(object) {
spanId = object.id;
};
now spanId will have the current value.
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.
function showHideSoldTo() {
if ($("#radio-text-sold-to").prop("checked")) {
$("#select-sold-to").hide();
$("#text-sold-to").show();
} else if ($("#radio-select-sold-to").prop("checked")) {
$("#text-sold-to").hide();
$("#select-sold-to").show();
}
}
$("#radio-text-sold-to").click(showHideSoldTo());
$("#radio-select-sold-to").click(showHideSoldTo());
All this is inside a document ready wrapper.
Remove the () from the function names in the click call. So..
$("#radio-text-sold-to").click(showHideSoldTo);
...
Remove the () from the function names in the click call
just try like
$("#radio-text-sold-to").click(showHideSoldTo);
$("#radio-select-sold-to").click(showHideSoldTo);
The problem is with the value you pass to the .click() method it's supposed to be a function value but instead you are invoking the function and as a result passing the return value of that function (which is equal to undefined)
the fix is simple you need to remove the () in these two lines
$("#radio-text-sold-to").click(showHideSoldTo());
$("#radio-select-sold-to").click(showHideSoldTo());
So they become
$("#radio-text-sold-to").click(showHideSoldTo);
$("#radio-select-sold-to").click(showHideSoldTo);
in javascript any identifier identifies a value. That value might be a simple value such as an integer or it might be a more complex object and as is the case with the value you pass as a callback or event handler it might be a function value.
These two lines of code are basically the same
function myFoo() {}
var myFoo = function() {}
and in the latter it's explicit that the right hand side is assigned to the left hand side. Ie that the function value is assigned to an identifier.
Below is my code fragment:
<div onclick = "myClick('value 1')">
button 1
</div>
<div onclick = "myClick('value 2')">
button 2
</div>
Basically when I for each click on a different div, a different value will be passed to the JavaScript function.
My Question is how can I keep track of the value passed in the previous click?
For example, I click "button 1", and "value 1" will be passed to the function. Later, I click on "button 2", I want to be able to know whether I have clicked "button 1" before and get "value 1".
Just add it to a variable in your script:
var lastClicked;
var myClick = function(value) {
lastClicked = value;
};
You can define somekind of variable, like var lastUsed;
add additional line to your function:
var lastUsed = null;
function myClick(value){
prevClicked = lastUsed; //get the last saved value
...
lastUsed = value; //update the saved value to the new value
...
}
And here you go
You need a variable. Variables are like little boxes in which you can store values. In this case, we can store the value that was last passed to the function myClick.
In Javascript, you can define a variable like this:
var lastClickedValue;
You can "put" a value into that variable. Let's say you want to put your name in there. You would do this:
lastClickedValue = 'sams5817';
Now here's the tricky bit. Variables have "scope". You might want to think about it as their "life-time". When a variable reaches the end of its scope, you cannot read or write to it anymore. It's as if it's never been. Functions define a scope. So any variable you define in a function will disappear at the end of the function. For example:
function myClick(value)
{
var lastClickedValue;
alert('lastClickedValue is = ' + value);
lastClickedValue = value;
}
That looks almost right, doesn't it? We declared a variable, display its last value, and update it with the new value.
However, since the lastClickedValue was declared in the function myClick, once we've reached the end of that function, it's gone. So the next time we call myClick, lastClickedValue will be create all over again. It will be empty. We call that an "uninitialized" variable.
So what's the problem? We're trying to remember a value even after the end of myClick. But we declared lastClickedValue inside myClick, so it stops existing at the end of myClick.
The solution is to make sure that lastClickedValue continues to exist after myClick is done.
So we must delcare lastClickedValue in a different scope. Luckily, there's a larger scope called the "global scope". It exists from the moment your page loads, and until the user moves on to another webpage. So let's do it this way:
var lastClickedValue;
function myClick(value)
{
alert('lastClickedValue is = ' + value);
lastClickedValue = value;
}
It's a very small difference. We moved the declaration of the variable lastClickedValue to be outside the function myClick. Since it's outside, it will keep existing after myClick is done. Which means that each time we call myClick, then lastClickedValue will still be there.
This will let you know what the last value passed to myClick was.
Finally, I'd like to advise you to look for some kind of Javascript tutorials. I wish I knew of some good ones to recommend, but I'm certain you can find a few on the Internet. If you try to write programs before understanding what you're doing, you'll find yourself producing work that is less than what you're capable of. Good luck!
I suppose you need something like this
var clickedButtons = [];
function myClick(value){
...
clickedButtons.push(value);
...
}
I am surprised that no one else mentioned this, but since functions are first class objects in JavaScript, you can also assign attributes and methods to functions. So in order to remember a value between function calls you can do something like I have with this function here:
function toggleHelpDialog() {
if (typeof toggleHelpDialog.status === 'undefined')
toggleHelpDialog.status = true;
else
toggleHelpDialog.status = !toggleHelpDialog.status;
var layer = this.getLayer();
if (toggleHelpDialog.status) layer.add(helpDialog);
else helpDialog.remove();
layer.draw();
}
Here I have added an attribute named 'status' to the toggleHelpDialog function. This value is associated with the function itself and has the same scope as the toggleHelpDialog function. Values stored in the status attribute will persist over multiple calls to the function. Careful though, as it can be accessed by other code and inadvertently changed.
we can leverage javascript static variables
One interesting aspect of the nature of functions as objects is that you can create static
variables. A static variable is a variable in a functionâs local scope whose value persists across
function invocations. Creating a static variable in JavaScript is achieved by adding an instance
property to the function in question. For example, consider the code here that defines a function
doSum that adds two numbers and keeps a running sum:
function doSum(x,y){
if (typeof doSum.static==='undefined'){
doSum.static = x+y;
}else{
doSum.static += x+y;
}
if (doSum.static >= 100){doSum.static = 0;doSum.static += x+y;}
return doSum.static;
}
alert(doSum(5,15))
alert(doSum(10,10))
alert(doSum(10,30))
alert(doSum(20,30))
This confused me a lot
var show = function(){
console.log('wow');
};
var show2 = function(word){
console.log(word);
};
button_element.addEventListener('click', show2('wow'), false)
'wow' //it return the string immediately but hit the button, nothing output in the console,
but
button_element.addEventListener('click', show. false)
No string return as we expect and then hit the button wow shows in the console as intended
anybody explain why does it act this way?
addEventListener takes a function as its second argument. Once you call a function, it means you're passing the returned value of that function, not the function itself. If you want to use a function call in addEventListener like you're trying to do, you should add a nested function that gets returned when you call it.
Try this:
var show2 = function(word){
return function(){ console.log(word); }
};
button_element.addEventListener('click', show2('wow'), false);
In the example above, the call to addEventListener is sent the inner function, which will have the captured value of the word variable within it.