Why is 'indexOf' not returning anything? - javascript

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

Related

Jquery, 'invalid or unexpected token'. Not a string issue

I'm a newbie to javascript and jquery, and this bug has been hounding me for almost a day. Here's the code:
$('#txtTabContentsHeight', '#ddlTabContentsHeightRuler').blur(function ()
{
//need to check ruler val first!
var sruler = $('#ddlTabContentsHeightRuler').children("option").filter(":selected").text‌​().toLowerCase();
var sval = $('#txtTabContentsHeight').val(); + '' + sruler;
ChangeTabContentsCss(this, 'height');
});
I get a jquery error in my browser console on the $('#ddl...').^^children(... line. Even if I do a $('#ddlTabContentsHeightRuler').val(), I get the same error on the '.' of invalid or unexpected token.
I've checked every single similar discussion here and on the web for this kind of error, and it's always about a string issue, or a syntax issue. But for the life of me, I can't see a syntax problem here. The ddl element is a select element, obviously.
Can someone smack me upside the head with an answer?
Try changing .text() for .val() or .html(). The .text() method cannot be used on form inputs
did u need the semi-colon on $('#txtTabContentsHeight').val(); + '' + sruler;or
$('#txtTabContentsHeight').val() + '' + sruler;
Well, as usual when I have these kinds of errors, if I just RETYPED the whole line, the bug disappeared! I actually did an undo, and tried to delete every single character on the original line, to see if I had some 'hidden characters' or something...and NOPE. Still got the error, though the line looked EXACTLY the same as the one that works.
Is this usual with jquery/javascript? That's sort of unacceptable as a programmer...
As earlier suggested, you could use with cross checking your html code especially the id names of the elements being selected

JSON return error as undefined when the value is numeric

not sure where the errors lies in what I am trying to achieve.
I am working with someone else's code and unfortunately they have used numbers for div ids in some places.
These number ids are used in various places and if I can, I want to find a way to keep things as they are.
So,
returning the following in JSON:
editorID: "1000"
And in my AJAX call i use that return like so:
var editorID = response.editorID;
CKEDITOR.instances.editorID.insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');
However this gives me an error saying that the editorID is undefined.
As you can I already use a JSON response in my code, this works fine so its not a problem with datatypes etc.
I also tried to do:
alert(response.editorID);
which gave me the correct value.
When I tried putting a number directly into CKEditor insertHTML code it was showing my syntax errors so maybe thats the issue. If so, any work around for it?
Thanks. Craig.
To use a variable as a property, you have to use [] notation:
CKEDITOR.instances[editorID].insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');
When you use .editorID, it's looking for a property named editorID, not 1000.
You also have to use this syntax when the property isn't a valid identifer. So if you wanted to put the number directly, you would write:
CKEDITOR.instances['1000'].insertHtml('<br><img class="buildimage" src="http://www.buildsanctuary.com/phpLibs/bulletproof-master/src/userBuildImages/'+response.imageName+'"><br>');

Javascript json, check all keys for undefined ('null') and set default

Firstoff I'd like to add I've been learning javascript for like only 2 days now. I'm pretty much way ahead of myself with what I'm trying to get but here goes.
I have a json array from which I get data to replace/insert in my page. The first problem I have is that if it comes across an empty ('null') key it will just stop. Will not even try to continu.
document.getElementById("id1")src=json.img1.link;
document.getElementById("id2")src=json.img2.link;
document.getElementById("id3")src=json.img3.link;
json.img2.link is empty ('null' response from json.). javascript will then not replace "id2" but it also won't replace "id3".
I'm now trying to find a solution where it will if nothing else at least set a default.
The script is not continuing executing because it comes to an error --trying to access property link of an undefined object
Try
document.getElementById('id2').src = json.img2 ? json.img2.link : 'defaultLink';
This way your are checking for undefined (ie null) object in img2 and assigning the default value. This assumes that what is not defined (null) is the img2 object.
Actually I don't think your code should work at all, you are missing the. before the src So, try
document.getElementById("id1").src=json.img1.link;
document.getElementById("id2").src=json.img2.link;
document.getElementById("id3").src=json.img3.link;
and let us know if that doesn't solve the problem.
Btw, +points for learning JavaScript and not just straight into jQuery!

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

JavaScript if statement returns wrong values

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

Categories

Resources