javascript return, not functioning [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code:
replaceAny('this','that',string);
if(val!="")
the replaceAny function looks like this:
function replaceAny(first,second,ele) {
var val = ele.replace(first,second);
alert(val);
return val;
}
But then after running the replaceAny function (and the alert shows the right value, the if condition tells me that the variable val is not set, why?!

You are not using the return value from the function correctly. This is something like what you want
function replaceAny(first,second,ele) {
return ele.replace(first,second);
}
var val = replaceAny('this','that',string);
if(val!=""){
//something, something darkside
}

I think you are not assigning the value returned by your function to your variable val.
Try like this
val = replaceAny('this','that',string);
if(val!="")

Related

For loop JS for a calculator [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to create a calculator which goes through 18 units. I wanted to make my code shorter by using a for loop. I thought something like this would work:
var i=0;
for (i=0;i<=18;i++)
{
if (Unit[i] = "P" or Unit[i] == "p")
{
UnitTotal[i] = 70;
SetCookie('UnitAns'[i],UnitAns[i]);
}
}
This doesn't work what am I doing wrong or what do I need to do differently?
Unit[i] = "P"
Unless it throws an exception because Unit isn't defined, this will always be true. = is an assignment, not a comparison.
or
or is not a keyword in JavaScript. The OR operator is ||.

How to make the following JavaScript code work and result in a list of names and ages inside the element with ID "myDivId" : [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to make the following JavaScript code work and result in a list
of names and ages inside the element with ID "myDivId" :
<div id="myDivId">No results yet</div>
<script>
new PersonPrinter("myDivId",[{"name":"Joe","age":"23"}
{"name":"Sam","age":"31"}]).render();
</script>`
In the PersonPrinter render function you need to update the div
PersonPrinter.prototype.render = function(id,data) {
var elem = document.elementById(id);
for (var i =0;i <data.length;i++) {
var d = dcoument.createElement('div');
d.appendChild(document.createTextNode('Name'+data.name +' Age:'+data.age');
elem.appendChild(d);`enter code here`
}
}
You need to access div id using innerHTML
var a = Document.getElementById('myDinId');
And for changing value use simply = operator
a.innerHTML = 'Something';

Javascript declaration [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi this is a simple question. I was wondering is there any different when you declare something like this. Thanks
selectedData[key](val)
and
selectedData[key] = val
This line selectedData[key](val) is not a declaration, it's calling the function that is stored under the key key in the object selectedData and it's passing the parameter val to that function.
The other line selectedData[key] = val is assigning the value val to the key key in the object selectedData.
In the first case, you're calling whatever is in selectedData[key] as a function with val as an argument while in the second one you're assigning it.

How do I catch the response from a named function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm calling a function like this:
$('.qa-nav-main-ask a').click(showAskForm);
Show showAskForm is this (simplified):
function showAskForm(){
return true;
}
I want to catch the response from showAskForm and save it to a variable, I tried
askFormShowing = $('.qa-nav-main-ask a').click(showAskForm);
without success.
function showAskForm(){
return true;
}
var askFormShowing;
$('.qa-nav-main-ask a').click(function(){askFormShowing = showAskForm();});
Try the above
return is a reserved word and cannot be used as a JavaScript identifier. So, use like this:
$('.qa-nav-main-ask a').click(showAskForm);
function showAskForm(){
myvar = true;
}

How to find a certain class using .find() or some jQuery Syntax? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
if( hasWhiteSpace($(this).find(".nospace:input").val()) ){
$(this).find(".nospace:input").filter(function() {
return hasWhiteSpace(this.value) == true;
}).addClass("blank");
setError(".motd_register_company","There must be no space in between.");
return false;
}
There is something wrong about my code above, and the code only validate the first input, while the others not, how could I validate all the "nospace" class that I have in my form?
can you guys trim my code if there is something wrong.
Are you trying to recursively search elements for a particular classname? If so:
$('input.class_name').each(function() {
// Do you stuff to this input element
});
jQuery.each("input.nospace", function(i, el){
var self = $(el);
if(hasWhiteSpace(self.val()) self.addClass("blank");
});

Categories

Resources