JQuery set input box value to this.value onclick? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
$("#something1").click(function(){
$("#something2").val(this.val);
});
I'm trying to set the value of something2 to the value of something1 onclick ...

this.value not this.val
$("#something1").click(function(){
$("#something2").val(this.value);
});
this.val will be evaluated to undefined. And it will be passed into the .val() function. Eventually it will be ignored.
Technically, in the .val()'s function definition, the logic would be, if it was called without any parameter, then the value of the current element over which the .val() was called will be returned.
calling $("#something2").val(undefined) will be similar to $("#something2").val()

Or use the jQuery val function:
$("#x").click(function(){
$("#y").val($(this).val());
});

Related

JavaScript function printing itself rather than returning value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to return a Boolean value based on truthy of a given condition from an arrow function. The code is look like following
checkIsBasicInformationCompleted() {
const info = this.basicInformation;
const valid = () => {return !!(info.firstName && info.lastName && info.email && info.phone);};
console.log(valid);
},
But here instead of returning true/false, this function is printing itself. Can anybody explain the thing running here ? And how can I get a true/false value here ?
Fiddle sample: https://jsfiddle.net/tebz4Lc3/
You are printing a reference to the function here, use console.log( valid() ) to actually execute the function and print the return value.
console.log(valid());
This is how the logging should be.

Why does JavaScript get this comparison wrong? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is happening on an angular application I'm building. If a user enters 80 into an HTML input, it always seems to get this comparison wrong.
var x = '80';
var y = 150.9800;
/* Returns incorrect answer */
if (parceFloat(x) < y) {
return true;
} else {
return false;
}
You need to use ParseFloat() not parceFloat() ...
parceFloat is not an existing function.
parceFloat() is not a function, the function is parseFloat()
A simple typo is all the error there is.

Dealing with javascript objects [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a function in javascript as so :
function player(){
var cards=[];
this.score=0;
var self=this;
this.addCard=addCard;
this.resetCards=resetCards;
function addCard(card){
cards.push(card);
this.score=+card.value;
}
function resetCards(){
cards=[];
score=0;
}
}
I user a constructor to call the function :
var player1=new player();
Then I call some of its enclosed functions like this
player1.addCard(someCardObject);//card someCardObject has .value say 5
player1.addCard(someCardObject);//card someCardObject has .value say 7
I expect player1.score to be 5+7=12 ,but it stays 7 .
Can anyone tell me what I am doing wrong here
You've got a simple error in addCard.
this.score=+card.value;
Should be
this.score += card.value;
In the first instance, you're setting this.score equal to card.value, while in the second one, you're adding card.value to it. Remember kids, order of operators matters!

JQuery Mobile testing a data- attribute for boolean value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a list of li tags in an unordered list and each li has an attribute called data-clicked=false. this if... else if... statement will test to see if data-clicked==true/false and then give an alert with the value of the attribute. The problem is that this is always alerting "true" when it should be "false" since by default at the start they are all set to false.
$("ul").click(function(event) {
if ($(event.target).data("clicked"==true)) {
alert("true")
}
else if ($(event.target).data("clicked"==false)) {
alert("false")
}
});
basically all I want to know is how I can test to see if the data-clicked attribute is true or false.
your condition
if ($(event.target).data("clicked"==true)) {
wont work. It should be
if ($(event.target).data("clicked")==true) {
and same would be the case for the else if part

How to add property to a global Javascript object inside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Categories

Resources