I must be overlooking something simple here. I cant get getElementById() to act on the variable passed to it.
function newforminput() {
var jsonstring = JSON.parse(document.getElementById('jsonobj').innerHTML);
var numfields = jsonstring.tracelog.fields.length;
for (var i=0; i<numfields; i++){
var field = i+jsonstring.tracelog.fields[i].name;
if (jsonstring.tracelog.fields[i].dependantfield != "---"){
document.getElementById(field).readOnly = true;
}
console.log(field);
}
}
console.log returns the correct ID of the div i want to affect, but the console tells me document.getElementById(field)... is null. Why is it not receiving the variable?
I'v seen a lot on this, but nothing seems to be working for me.
console.log(field) renders "1SectionName" - or whatever else its required to. If the element wasnt created yet, wouldn't the error be document.getElementById(1SectionName)... is null? That is, if the variable was being passed.
you were so right. A syntax error was prohibiting the id of the element from being created, so no element of that id existed hence the null error. Sorry for wasting your time due to a syntax error, but your input certainly helped me analyze my code more intelligently. Thanks for not ridiculing seemingly simple mistakes (as i often witness elsewhere) and providing meaningful insight for us newbies. thanks again!!!
Related
Hi Im a beginner so please forgive any basic mistakes. I am trying to learn java script online and so its read this and teach yourself I am working through one of the projects and am confused as I have defined a variable called it one and it prints the string then later called it again in another string and it shows it as undefined. here is my code.
let userName = 'Steve';
userName = userName ? console.log(Hello ${userName}.) : console.log('Please enter your name');
let userQuestion = 'why are we here';
console.log(You ${userName}, would like to know, ${userQuestion});
this all works as I would expect apart from the last line that returns
You undefined, would like to know, why are we here?
I am aware undefined is returned when nothing is set to the variable but the variable is already set and works the first time I have even removed the first instance out and ran it again the issue still happens.
any help would be very much appreciated
You're reassigning userName to the return value of console.log() which is undefined
My code is intended to make editing easier in Google Docs, but doesn't quite work and returns getBody null. I've already tested setting "body" as a variable, but that doesn't quite work. Due to how inexperienced I am with Javascript, it's hard to wrap my head around why exactly getBody returns null. So, it'd help me for the future if you knew where I'm going wrong with this.
function myFunction() {
var searchResult
searchResult =
DocumentApp.getActiveDocument().getBody().findText("very",searchResult)
Logger.log(searchResult)
while (searchResult !== null)searchResult.getElement().asText().setAttributes(searchResult.getStartOffset(),searchResult.getEndOffsetInclusive(),"#FF000")
searchResult =
DocumentApp.getActiveDocument().getBody().findText("very",searchResult)
}
function highlightProblem() {
var words = ["very","so","totally","really"]
words.forEach(findText)
}
function onOpen(){
DocumentApp.getUi().createMenu('everythingisnotfine.avi').addItem('Higlight Words That Make You Sound Like a Dandy', 'higlightProblem').addToUi()
}
Your body is not null. Your first problem is in this line
searchResult = DocumentApp.getActiveDocument().getBody().findText("very", searchResult)
you have findText("very", searchResult) while you do not have the variable searchResult. As per documentation this requires a range class (read here)
Next up is the whole mess here
while (searchResult !== null)
searchResult.getElement().asText()
.setAttributes(searchResult.getStartOffset(),
searchResult.getEndOffsetInclusive(),
"#FF000"
)
I did some formatting to better see the whole while loop. First of all, this is an endless loop, because it will keep repeating as searchResult is never changed inside of the loop so this step is either skipped or will continue endlessly. Next up is this particular method:
.setAttributes(searchResult.getStartOffset(),
searchResult.getEndOffsetInclusive(),
"#FF000"
)
Please read up on the method here as you are using it incorrectly. You are providing a string to the method, where it expects an object. In the same documentation page scroll down to the next method, without the offsets, it shows an example of how attributes should be formatted.
Next we move outside of the loops. This seems to be pointless
searchResult = DocumentApp.getActiveDocument().getBody().findText("very",searchResult);
perhaps you meant it to be done inside the while loop? Because essentialy what you are currently doing is
Set searchResult from document.
Log searchResult object
Set searchResult attributes (since that looks like hex color code, I assume you wish to color the code). Do this while searchResult is not null.
Set searchResult from document
End
If this was meant to be inside the while loop then you need to add { after while and } after the last action you wish to happen in 1 iteration of the while loop.
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!
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
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