JavaScript if statement returns wrong values - javascript

Does anybody knows why this snippet returns false even if the passed string is "Active"?
if ($('status_'+id).getText()=="Active")
I've also tried changing the code to
if ($('status_'+id).getText()==String("Active"))
and
if (String($('status_'+id).getText())=="Active")
and still no luck.
I've also checked $('status_'+id).getText() through console.log to verify if it really returns "Active"
i wonder why it doesnt work?
any ideas?

Silly question: are you sure the returned string doesn't contain spaces?

The first step in any debugging task is to check your assumptions. Use a debugger or a series of alerts to check the following:
what's the value of id?
does$('status_'+id) evaluate to a DOM
element?
what does
$('status_'+id).getText() actually
return

Related

Using a for loop in JavaScript to loop over an array of objects

My code and terminal
Just ignore the comments, they are in danish as it is for a school assignment..
I need some help. My loop keeps giving me an undefined value in my terminal, and I can't seem to find the issue. It is working as it should, and gives me the correct values, but still has those 'undefined' ones which irritates me.
EDIT: Has been fixed by #aqq, thx for the help everybody!
As #dikuw mentioned the undefined call might be coming from the gørBeskedPersonlig function being called.
I don't see that function being defined in your code so that's probably it, try commenting out the following line:
console.log(gørBeskedPersonlig(katalog[index].navn));
UPDATE: After reviewing your code, i can see that the function gørBeskedPersonlig was not returning anything.
Updating it to return the new value has fixed the "undefined" error.
function gørBeskedPersonlig(navn){
hemmeligBesked.splice(1,1,navn+'!');
return hemmeligBesked.join(' ');
}
It seems that you have some console.log() in function gørBeskedPersonlig that is returning undefined. Send a code for that function.

Serlialize array

I'm trying to serialise my array but I'm getting nothing in the log.
I want all checkboxes called color[] but not the first one.
I've tried:
$("input[name='color[]:not(:first)']").serialize();
But this logs as blank.
The following works but includes the first one, so it must be a problem with the not part.
$("input[name='color[]']").serialize();
Your attribute equal selector is wrong $("input[name='color[]:not(:first)']")
$("input[name='color[]']:not(:first)").serialize();
The selector you are using is wrong.
Finish ] after the name=value selector.
$("input[name='color[]']:not(:first)").serialize();
Here are the mistakes highlighted:
$("input[name='color[]:not(:first)']").serialize();
// ^ finish selector here
Please use this
$("input[name='color[]']:not(:first)").serialize();

javascript if condition not executing all commands

I'm having one little iritating problem. I have simple if condition in javascript code.
It goes something like this:
if (istinito)
{
alert ('123');
document.getElementById('obavestavanje').value="Pobedi "+ime_igraca+"!!!";
kraj=true;
}
Alert apears when istinito=true, but element with id="obavestenje" never get its value, and variable kraj never is set to true. Variable kraj is global variable, and there are no conflicts with other parts of the JS code.
Any ideas why code stops after alert?
Looks like document.getElementById('obavestavanje') is returning null. You are trying to de-reference the null reference by using document.getElementById('obavestavanje').value which results in null pointer exception. If you look into the console, you should see some exception being raised. It is always a good idea to check if the document.getElementById() is returning a valid object before trying to dereference it.
e.g.
if (istinito)
{
alert ('123');
element = document.getElementById('obavestavanje')
if(element){
element.value="Pobedi "+ime_igraca+"!!!";
}
kraj=true;
}
First advice i could give you:
Use more console logging for debugging. Almost any modern browser got a console to debug and other things.
if (istinito) {
console.log("i am here");
}
from that same console you can also execute commands. Those dom manipulations are easily done from the console. just run them and see if it works.
the code:
document.getElementById('obavestavanje').value = "some value"
looks ok. nothing wrong with it. i guess you don't have an element with id "obavestavanje" ?
Looks like your code is okay. And you are sure you have an element by id 'obavestavanje'. Could you please tell what element is it? Is it a button, textbox or someting like that?
Also the String in the "Pobedi "+ime_igraca+"!!!" , what is 'ime_igraca'? Is it a variable and if it is have you defined this variable somewhere?
Or did you mean to give the value "Pobedi ime_igraca !!!" ??
Thanks
Ranis MK

Why is 'indexOf' not returning anything?

The book I am reading tells me to open up the JavaScript console and try the code "foo: bar".indexOf(":"). I've tried it in many ways. I tried removing quotation marks, putting it inside a show() and alert() function. I just can't seem to tease anything out.
Has something changed in JavaScript? Has the author made a mistake? Am I supposed to get no return? Do I need to append document.write, perhaps? Any help greatly appreciated.
Yes something changed in Firefox 5+
However the console (ctrl-shift-k) still works
In the error console (ctrl-shift-J) you will need to wrap it in alert:
foo:bar is a property definition in json, and indexOf is supposed to deal with a left value (string variable, constant, or at least something that can have characters in it. I don't know why the book you are reading wants you to do this, but it doesn't seem to be correct. The correct way to use indexOf would be :
var myObject = {
foo:"bar"
}
alert(myObject.foo.indexOf("a"));
try like follows, it should work. Generally the indexOf() will return -1 if the value to search for never occurs.
var str="foo:bar";
document.write(str.indexOf(":") + "<br />");
The output should be 3

Prototype 1.6.0.3 - "Insert is not a function"

I'm going berserk with this. Although http://www.prototypejs.org/api/element/insert is far from being the best documentation page ever, I struggle with a really stupid simple implementation:
$('account').insert({'top':new Element('a')});
I also tried with a plain HTML string instead of new Element(a), but it doesn't change anything... Can you spot what's wrong with what I'm doing ?
Prototype returns null from $("foo") if no element with "id" value "foo" is on the page. If you're using the "id" value "account" on multiple elements, anything might happen, so don't do that. Otherwise make sure there's an element with "id" value "account" on the page when that code runs.
In JavaScript, the semicolon terminates a statement. You don't want to terminate the statement there, you wanted to call .insert on the result of $('account'), so don't put a semicolon there.
According to the documentation you linked, you're also missing a set of curly braces and some quotes.
$('account').insert({'top': new Element('a')});

Categories

Resources