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'm doing my first project using AngularJS and now i've run into a JavaScript problem that just can't understand. Perhaps this is trivial to someone more experienced in JS, if so i hope you guys can help me :)
I've doing this string comparison in function, that receives an id passed as a parameter from a view, which it then compares to id of objects kept in the service:
for (var i = 0; i < this.Sessions.length; i++) {
if (this.Sessions[i].sessionId === sessionId) {
for (var j = 0; j < this.Sessions[i].Instances.length; j++) {
if (this.Sessions[i].Instances[j].instanceId === instanceId);
{
console.log("InstanceId's: " + this.workoutSessions[i].Instances[j].instanceId + " " + instanceId);
//Do stuff and return.
}
}
}
}
When i run the method all the instanceId's of all the instances will evaluate to true.. Why??? As far as i know i'm just comparing strings, but i guess not. The outer comparison works as it should, which makes it even stranger to me.
Output in console, showing that they evaluate to true no matter the value of the strings.
InstanceId's: l0h34qzzgdlpu8fr 42p9smh9kxdsfw29
InstanceId's: 42p9smh9kxdsfw29 42p9smh9kxdsfw29
Really what i want to do is stop the function as soon as i've found the correct instance, but if i put in a return statement inside the IF-block my compiler tells me the j++ statement is unreachable, indicating no matter what the expression is going to be true, and... that is just beyond me.
How the parameter is passed from the view:
<div ng-repeat="instance in session.Instances">
//Some form controls here...
<div style="float: right;">
<button class="btn btn-default" ng-click="addOne(session.sessionId, instance.instanceId)"> + 1</button>
<button class="btn btn-default">Remove</button>
</div>
</div>
Can anybody help??
Thanks in advance.
if (this.Sessions[i].Instances[j].instanceId === instanceId);
the colon at the end seems to be the culprit...
delete the semicolon in the if clause.
Your Code will be same as
if (this.Sessions[i].Instances[j].instanceId === instanceId)
{
;
}
{
console.log("InstanceId's: " + this.workoutSessions[i].Instances[j].instanceId + " " + instanceId);
//Do stuff and return.
}
Related
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 am working on a Blazor server project and have this button in my component
<button type="button" class="btn btn-primary" #onclick="#GetUserInput">Add Name</button>
Here is the code I have that the click event is calling
protected async void GetUserInput()
{
var result = await JsRuntime.InvokeAsync<string>("getName");
}
function getName() {
var retVal = prompt("Enter Name : ", "name here");
return retval;
}
In the javascript function, retVal has a value but in the GetUserInput method,
the code crashes with the error message below
ReferenceError: retval is not defined
at getActivityName (https://localhost:44318/script.js:15:11)
at https://localhost:44318/_framework/blazor.server.js:1:70045
at new Promise (<anonymous>)
at e.beginInvokeJSFromDotNet (https://localhost:44318/_framework/blazor.server.js:1:70011)
at https://localhost:44318/_framework/blazor.server.js:1:26293
at Array.forEach (<anonymous>)
at e.invokeClientMethod (https://localhost:44318/_framework/blazor.server.js:1:26263)
at e.processIncomingData (https://localhost:44318/_framework/blazor.server.js:1:24201)
at e.connection.onreceive (https://localhost:44318/_framework/blazor.server.js:1:17286)
at WebSocket.i.onmessage (https://localhost:44318/_framework/blazor.server.js:1:46503)'
I am not sure what is going on? Any ideas?
Javascript is case sensitive. retVal, not retval - JS won't complain about "retval" - it's just undefined.
function getName() {
var retVal = prompt("Enter Name : ", "name here");
return retVal;
}
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 5 years ago.
Improve this question
I am testing a line of code where it checks a variable if it is empty or not. If its empty a prompt pops up. But even if the variable has something, the prompt still pops up.
function myFunction(){
if (site == null || variable == undefined) {
var site = prompt("Please enter a valid url:", "http://");
document.cookie = 'Your bookmark is: '+ site;
alert(unescape(document.cookie));
document.getElementById("p1").innerHTML = '<a class="txt2" href="' + site + '" target="myframe">' + site + '</a>';
}
else {
alert('yey its working');
}
}
<a class="txt2" id="p1" onclick="myFunction()">Button</a>
The code works it's just after the variable is set the prompt pops up again before it loads the page.
It is because variable is undefined (we are not setting it anywhere), the if block always runs.
Also, if site variable seems defined in the scope of myFunction -
Variables declared within a JavaScript function, become LOCAL to the
function
Ideal way is to check for document.cookie instead of variables -
function myFunction(){
if ( document.cookie == '') {
var site = prompt("Please enter a valid url:", "http://");
document.cookie = 'Your bookmark is: '+ site;
alert(unescape(document.cookie));
document.getElementById("p1").innerHTML = '<a class="txt2" href="' + site + '" target="myframe">' + site + '</a>';
}
else {
alert('yey its working');
}
}
myFunction();
Check it working in JSFiddle
Your if statement evaluates two variables which only either one has to be true in order for the if statement to be true.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this function which produces the correct value when run, but I am having a hell of a time displaying the results.
Here is the JS which is calculated onChange in a form I am trying to display the resulting value elsewhere on the form. The alert displays the correct value but my id remains blank.
Thanks in advance for taking a look
function calculate_mtow1() {
togw_n = 0;
togw = $('#togw').val();
if (togw != '' && togw != 0 && togw != 'Nan') {
var togw = togw.replace(",", "");
togw_n = togw;
}
burn_n = 0;
burn = $('#burn').val();
if (burn != '' && burn !=0 && burn != 'Nan') {
var burn = burn.replace(",", "");
burn_n = burn;
}
var mtow1 = parseInt(togw_n) + parseInt(burn_n);
$('#mtow1').val(mtow1);
document.getElementById('mtow1');
alert(mtow1);
}
<td>TOW + Fuel Burn =<span id="mtow1"></span></td>
Your code is getting the element with getElementById but then not doing anything with it. You need to assign the result of getElementById to something, or call methods on it on the same line. If your goal is to put the value of mtow1 into your <span>, try doing this:
// Solution 1
var spanElement = document.getElementById("mtow1");
spanElement.innerHtml = mtow1;
Alternatively, perhaps you were trying to display the value of mtow1 by using this jQuery:
$('#mtow1').val(mtow1);
That doesn't do what you think it does. It changes the "value" attribute of the span to the value of mtow1, but that change isn't visible to the user. It's the same as writing this as your HTML:
<td>TOW + Fuel Burn =<span id="mtow1" value="valueofmtow1"></span></td>
If you want to use jQuery instead of the getElementById method I posted above, you could do this:
// Solution 2
$('#mtow1').html(mtow1);
You don't need to do both. Either solution will work on its own.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I'm trying to lookup a field's value, if it equals '1', then put the value '1' in different field, if not put a '0'.
I'm not sure why this isn't working, can anyone help?
<input type="text" name="_1_1_33_1_id" value="" onchange="checkLineManager();">
<input class="valueeditable" type="text" name="_1_1_118_1" id="_1_1_118_1" value="" >
Javascript:
function checkLineManager() {
if (document.getElementsByName('_1_1_33_1_id').value == '1') {
document.getElementById('_1_1_118_1').value = '1';
} else {
document.getElementById('_1_1_118_1').value = '0';
}
}
http://jsfiddle.net/nbren007/o9xp0efy/
Note the plural use of "elements" in the following line:
if (document.getElementsByName('_1_1_33_1_id').value == '1') {
This doesn't return an element, it returns a node list.
// To confirm that
alert(document.getElementsByName('_1_1_33_1_id').toString());
So you need to use:
if (document.getElementsByName('_1_1_33_1_id')[0].value == '1') {
There are other ways of accessing the element as well. Most notably through the form element approach.
The hint is in the method: getElementsByName returns more than one element - it returns an array of matching elements.
You need to use array notation to select the element from the array.
Change:
document.getElementsByName('_1_1_33_1_id')
To:
document.getElementsByName('_1_1_33_1_id')[0]
if (document.getElementsByName('_1_1_33_1_id')[0].value == '1') {
document.getElementById('_1_1_118_1').value = '1';
} else {
document.getElementById('_1_1_118_1').value = '0';
}
Or even neater, use a ternary statement:
document.getElementById('_1_1_118_1').value =
document.getElementsByName('_1_1_33_1_id')[0].value == 1 ? '1' : '0';
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 am using a javascript to get data and put it into a table, i dont know where i have gone wrong? please help!
my code is:
function getSOCsForJobTitle() {
var searchtitle = s("#search-input").val();
var apiurl = "http://api.lmiforall.org.uk/api/v1/soc/search?q="
var apicall = apiurl + searchtitle;
s.get(apicall.function (data) s.each(data.function (i.e) {
var tablerow = s("<tr></tr>");
tablerow.append("<td>" + e.title + "/td>");
tablerow.append("<td>" + e.SOC + "/td>");
s("#SOCstable").append(tablerow);
});
});
}
s(function () {
// this gets called when the page loads
s("#search-go") onclick(getSOCsForJobTitle);
});
apicall.function (data) s.each(data.function (i.e) {
First:
apicall is a string, so it won't have a function property. Presumably you are trying to pass it and the function as separate arguments to s.get. You need a comma, not a period.
apicall, function (data) s.each(data.function (i.e) {
Second:
It looks like you are trying to pass an anonymous function as the second argument. The syntax for that is:
function (arg) {
// expressions
}
You are missing the {.
apicall, function (data) {
s.each(data.function (i.e) {
Third:
It looks like you've made exactly the same error for what you are trying to pass to s.each.
s --> $
s("#search-go") onclick --> $("#search-go").click