Do you know why this
function deleteInputOnClick(input){
champ = document.getElementById(input);
if(champ.value =='E-mail'){
champ.value = "";
}
}
works but this way
function deleteInputOnClick(input){
champ = document.getElementById(input).value;
if(champ=='E-mail'){
champ= "";
}
}
it doesn't ?
It's probably a stupid little error but I really don't see where it could be.
Thank's
You're not setting the value back on the element in the second way, you're just assigning it to a local variable, you still need to do document.getElementById(input).value = champ;
That is because is champ is a variable that has been assigned the value of the input element.
Changing its value will not change the value of the input (as it is not a two-way binding, you just assigned the value to it)
So you need to directly target the value property of the input to alter its value
If you are trying to avoid using the .value to a lot of places you can cache both the input element and its value for different uses..
function deleteInputOnClick(input){
var champ = document.getElementById(input),
value = champ.value;
if(value=='E-mail'){
champ.value = "";
}
}
document.getElementById(input).value returns a string value while document.getElementById(input) returns a reference (an object). So in one case only the value of the local variable is changed, in the other the original object still links to the DOM value.
Have a look at this question: Javascript by reference vs. by value
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'd like to do something like this:
function someFunction(expression){
eval(expression) = 1234;
}
Or in other words, to pass a certain field name as an argument and then set a value to this field.
How can I do that?
If you want to assign a value to a new variable with a variable name, in the global scope, you can just do this:
function someFunction(expression){
window[expression] = 1234;
}
There is really no good excuse to use evil()
I'm trying to assign a variable to another variable and try to do only one way binding. But when value is updated in view, it updates to original variable too. How do I stop this two way binding while assigning variable to another.
For example:
function personController ($scope) {
var templateValue= "original value";
$scope.myVal= templateValue;
}
In view:
<input type="text" ng-model="myVal" />
Result:
When we type something in textbox, it updates value in myVal and templateValue too, i.e value in templateValue changes to whatever I just typed in the input box. Is there a way to assign variable to another variable doing only one way binding? I want two way binding between $scope.myVal and the input box but not between templateValue and input box.
You can't "force one-way binding" because of the weay JavaScript works.
In your example, updating myVal will not actually update templateValue.
function personController($scope) {
var templateValue = "original value";
$scope.myVal = templateValue;
}
If you have the following structure, then yes, changing myVal.test will update templateValue.test because they both reference the same object in memory.
function personController($scope) {
var templateValue = { test: "original value" };
$scope.myVal = templateValue;
}
if you want myVal and templateValue to reference different objects but have the same content, make a copy of the original object:
$scope.myVal = angular.copy(templateValue);
I also suggest familiarising yourself with Javascript by reference vs. by value.
Consider the following code snippet:
inputTextField=document.getElementById("Phone_input");
var value = inputTextField.value;
value=value.substring(0,10);
where Phone_input is an <input type="text"/> element. Why during the running of this script there is no changes of actual value of the <input type="text"/>. We're changing value by the reference which indicates to inputTextField.value.
The variable value is not a reference, so after the change you must write it back into the textfield:
value=value.substring(0,10);
inputTextField.value = value;
Or, in one line:
inputTextField.value = inputTextField.value.substring(0,10);
Javascript always passes by value, but in an array or object, the value is a reference to it, so you can 'change' the contents.
In this case you have to do it this way:
var inputTextField=document.getElementById("Phone_input");
inputTextField.value = inputTextField.value.substring(0,10);
I am updating a text value and I'm not sure why the first block of code doesn't work, but the alternate block does. Can someone explain this to me? They seem equivalent.
//doesn't update
newAtomicNum = 2;
oldAtomicNum = document.getElementById("atomicNumber").firstChild.nodeValue;
oldAtomicNum = newAtomicNum;
*versus* //does update
newAtomicNum = 2;
oldAtomicNum = document.getElementById("atomicNumber");
oldAtomicNum.firstChild.nodeValue = newAtomicNum;
When calling nodeValue without setting it, it returns the current nodeValue, not a reference to the property.
So an element looking like
<div id="atomicNumber">test</div>
Where you call
var oldAtomicNum = document.getElementById("atomicNumber").firstChild.nodeValue;
oldAtomicNum now contains the string test, so setting the variable to something else does not update the elements nodeValue
They're very different indeed.
In your first example, all you're updating is the variable, oldAtomicNum. There's no enduring link between that and document.getElementById("atomicNumber").firstChild.nodeValue at all. Assignment (=) copies values, it doesn't create references.
Note that this is very different from this:
newAtomicNum = 2;
atomicNum = document.getElementById("atomicNumber").firstChild;
atomicNum.nodeValue = newAtomicNum;
...which updates the value. The reason that works is that on the second line, the value we're copying into atomicNum is an object reference. Then we use that reference on the third line, assigning to one of the object's properties.