can not parse getElementById [closed] - javascript

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.

Related

Trying to erase corresponding cell content based on value change of another cell on the same row [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 1 year ago.
Improve this question
This is the first time I am trying to create the equivalent of a macro in GSheet with Javascript (I am used to VBA macros in Excel).
I thought this short JS script would work once tied to the onChange event of my GSheet:
// When a cell in column BD (56) is changed to value 0 then, if the corresponding cell on the same row in column AR (44) has value 'Approved', clear the 'Approved' string.
function onChange(e) {
if(e.range.columnStart === 56) {
if(e.value=='0' && e.range.offset(0,-12).value=='Approved'){
e.range.offset(0,-12).clearContent();
}
}
}
BTW I do not get any errors. When I manually change the value to 0 in BD the Approved contentn in AR stays. I wish I knew how to debug this.
Thank you very much for your help!
The reason why your onEdit() is not working is because there is no value parameter in Range object. When you use Range.offset(rowOffset, columnOffset), It will return a Range object, to get the value of the cell you need to use Range.getValue()
Sample Working Code (With debug logs):
function onEdit(e){
Logger.log(e.range.columnStart);
Logger.log(e.value);
Logger.log(e.range.offset(0,-12).value);
Logger.log(e.range.offset(0,-12).getColumn());
if(e.range.columnStart === 56) {
if(e.value=='0' && e.range.offset(0,-12).getValue()=='Approved'){
e.range.offset(0,-12).clearContent();
}
}
}
Execution Log:
Notice that when you try to get the value of column AR using e.range.offset(0,-12).value, the value is null
Replace that with e.range.offset(0,-12).getValue() to get the cell's value
Note:
You can debug your trigger by adding logs then check the execution tab.

string === fails with equal strings [closed]

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
Edit: Not a duplicate as this is not a question about using === vs ==. Both of those methods fail.
First two lines from csv:
email,Product,Term,Rate
example#gmail.com,New,24 Months,1.99%
I have tried both === and == in an if statement but it always fails. Here is the code I use. First I create the array from a csv file. Then I loop through that array. When I echo on the second to the last line the output is example#gmail.com - example#gmail.com.
This works with if(line[0] === 'New'){ until I add in the email column in my csv sheet, but now even running if(line[1] === 'New'){ does not work if the email column is there. I checked again, and by removing the email column, it works fine. Everything is formatted as text in the csv.
casper.then(function readFile() {
var stream = fs.open('ck.csv', 'r');
var line = stream.readLine();
var lineArray = [];
while(line) {
lineArray.push(line);
line = stream.readLine();
}
casper.eachThen(lineArray, function(response) {
line = response.data.split(',');
this.echo(line[0] + ' - ' + email);
if(line[0] === email && line[1] === 'New'){
Make sure both email and line[0] trimmed. (Use email = email.trim())
You can also use the localeCompare() Method
If they are equal, you'll get 0. And we all know !0 is true.
http://www.w3schools.com/jsref/jsref_localecompare.asp
if(!line[0].localeCompare(email) && !line[1].localeCompare('New')){}

How to switch <td> content with another <td> [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
So the thing goes as following: I'm a begginer programmer (so far i know HTML, CSS some JavaScript and some C++) and we have a school project to create a chessboard with figures on them but I want to go a step further and be able to move the figures.
So far I've used the prompt function to get the coordinates and move them around but that feels far too much stone-agish. Now what I wish to accomplish is to be able to click on a , copy its content into a variable and upon clicking on another replace its content with the one stored in the variable. (I'll deal with the rules later..)
Each has its unique id and I have used element.firstChild.nodeValue to acquire the content.. Any suggestions on how to do this in JavaScript without using jQuery (if it can't be done in JavaScript then by all means do it in some other language.. it's about time I start learning them anyway.. :P)
If each <td> has a unique id, what about:
var lastStoredValue = "", lastClicked = ""; // = "" is important in that case
var t1 = document.getElementById("t1"); // td with id="t1"
var t2 = document.getElementById("t2"); // td with id="t2"
t1.onclick = function() {
if (lastClicked !== "t1" && lastStoredValue.trim())
t1.innerHTML = lastStoredValue; // replace its content with the one stored in the variable
if (!lastStoredValue.match(new RegExp(t1.innerHTML)))
lastStoredValue= t1.innerHTML; // copy its content into a variable
lastClicked = "t1";
}
t2.onclick = function() {
if (lastClicked !== "t2" && lastStoredValue.trim())
t2.innerHTML = lastStoredValue; // replace its content with the one stored in the variable
if (!lastStoredValue.match(new RegExp(t2.innerHTML)))
lastStoredValue= t2.innerHTML; // copy its content into a variable
lastClicked = "t2";
}
This gets what is stored into one td and puts it into a variable; then stores what is in the variable into another td when click on that td.

IF ElementID value then change another ElementID value [closed]

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';

compare two big strings with javascript [closed]

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 8 years ago.
Improve this question
I have two big strings A and B which store a HTML page. I want to compare those variables to see if pages are exactly same, Like if(A === B) alert("same"); else alert ("different");
but it fail to do such comparation , I suppose it is that because of very long string. How to compare in javascript such long strings ?
You can try to use MD5 hashes of long strings.
MD5 (Message-Digest algorithm 5) is a widely-used cryptographic hash
function with a 128-bit hash value. MD5 has been employed in a wide
variety of security applications, and is also commonly used to check
the integrity of data. The generated hash is also non-reversable. Data
cannot be retrieved from the message digest, the digest uniquely
identifies the data.
Try something like this :
var a = 'abasdfasfasd23141234123412';
var b = 'abasdfasfasd23141234XXXXXX';
function compare(a,b) {
var a_arr = a.split();
var b_arr = b.split();
if(a_arr.length != b_arr.length) {
return false;
} else {
for(var i = 0 ; i < a_arr.length ; i++) {
if(a_arr[i] != b_arr[i]) return false;
}
}
return true;
}

Categories