Problem adding Js variables in HTML with getElementById [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 4 years ago.
Improve this question
<body>
<h1>Operadores aritméticos</h1>
<h3>+ Adición (102+103)=</h3><p id="suma1"></p>
<h3>- Substracción</h3><p id="resta1"></p>
<h3>* Multiplicación</h3><p id="multi1"></p>
<h3>/ División</h3><p id="div1"></p>
<h3>% Módulo</h3><p id="mod1"></p>
<script>
var suma = (102+103);
var resta = (36-20);
var multiplicación = (27*30);
var división = (900/30);
var módulo = (106%3);
document.getElementById("suma1").innerHTML = suma;
document.getElementById("resta1")innerHTML = resta;
document.getElementById("multi1")innerHTML = multiplicación;
document.getElementById("div1")innerHTML = división;
document.getElementById("mod1")innerHTML = módulo;
</script>
Hello guys, I have a problem, im pretty new at this (programming with HTML, Js, etc.).The issue is that when I try to make my Js variables appear on HTML (with document.getElementById), they do not appear. Nevertheless, if erase every document.getElementById except the one containing "suma1", the browser displays me the result of the sum (205), but if I add even one of them, the browser doesn´t display anything.
I hope I was clear with my problem, it seems very simple but hard to explain.
Any suggestions?
Thanks in advance

The reason you're not seeing anything when you add the statements to populate resta1, multi1, div1, and mod1 is probably because they all have a syntax error. This is likely causing even the first statement (suma1, which is syntactically valid) not to work.
Valid Statement
The 1 statement that is working is document.getElementById("suma1").innerHTML = suma;
Invalid statements
All the other statements follow this pattern:
document.getElementById("id")innerHtml = variable;
Note that you're missing the . between getElementById("id") and innerHtml. If you add the missing . then it should all work as expected.

Related

? Incorrect regex expression [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I’ve been trying to do find if a variable contains a digit inside of it. And for some reason, I put (exp.) ‘Majorpassword09’ and it just prints a false?
// vars
var regex = new RegExp(‘.*\d.*’)
var str = ‘Scorpio08’
// main
function reg1() {
if (regex.test(str)){
console.log(‘true’);
}else{
console.log(‘false’);
}
}
reg1();
EDIT: I’ve put the title as Incorrect regex expression because that's the only thing that I think is wrong.
You're most likely on a mac.
Your code shows that you're using fancy quotes. Like these ones: ‘’. You need to use normal quotes, like ', and ".
Here's a solution to your problem.
// vars
var regex = new RegExp(".*\\d.*")
var str = "Scorpio08"
// main
function reg1() {
if (regex.test(str))
{
console.log("true");
}else{
console.log("false");
}
}
reg1();
Whatever text editor you're using, it has fancy quotes on. Consider looking in the settings, or on Google, to find out how to disable them.
as you are using string to create a RegExp Object thus have to use two backslashes to properly escape
var regex = new RegExp(‘.*\\d.*’)
or
var regex = /.*\d.*/

Why do I get an error when trying to console log my $.each [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
Sorry im new to jquery I usually use php/html. Can you tell why my code wont console log the VAR values. I am building an array from my vars when looping through them. I get the error Uncaught ReferenceError: $ is not defined.
Fiddle
var string1 = "tes£$%t";
var string2 = "test";
var string3 = "test";
var string4 = "test";
var check_fields = [string1, string2, string3, string4];
$.each(check_fields, function(index, value) {
if (value.replace(/^[a-z\d\-_\s]+$/i, "") != string) {
console.log(value);
}
});
Your code is fine but jquery file is missing here.
Fiddle link:- In the left side under the external resource just put the jquery cdn path and click on run then check.
https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js
This will resolve your problem.
include the jquery header file in the head your html code
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
Your html file is missing the external jQuery library.
Try adding this to the start of your html file (preferably in the head, of in the case of JSFiddle just paste it into the HTML box):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
Let me know if this works

Function can't find variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I was messing around with my game's CSS, and now I get a variable error for every button I click that calls a function, saying it can't find the variable.
I deleted the CSS and everything has changed, but the game is still broken.
I don't know whats wrong, so I have provided a link to download the code.
The error: reference error: can't find variable: duckClick
Related JavaScript:
var ducks = 0;
function duckClick(number){
ducks = ducks + number;
document.getElementById("ducks").innerHTML = ducks;
This calls duckClick but I'm getting a error saying it can't find it.
<button onclick="duckClick(1)">Find Duck</button>
Ah... a little does go a long way, especially in programming.
You forgot the curly closing duckClick, leaving it undefined.
function duckClick(number) {
ducks = ducks + number;
document.getElementById("ducks").innerHTML = ducks;
} //This curly brace
It seems like you have just forgot to close the duckClick-function with a } in your main.js file.

Need Array inside other Array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
For some reason I need to push an Array inside other array.
For example,
var a = ["Test1", 1];
var b = ["Test2", 2];
var c = [];
c.push(a);
c.push(b);
alert(c);
For this code I need the following output,
["Test1", 1],["Test2", 2]
But what I am getting is
Test1,1,Test2,2
Any help will be highly appreciable.
You've already done it correctly: c.push(a) and c.push(b) work, but you don't want to use alert() for debugging.
Though it might be more convenient since you don't have to open up a console, it's going to give you output that is inconsistent with the actual structure of the data because using alert(x) converts whatever x is to a string.
Always use console.log(). Had you done that in this case, you would have seen something like this in the console:
Demo

How to add property to a global Javascript object inside a function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 9 years ago.
Improve this question
I am tying to add a property to a JS object inside a function. I can do it outside but not inside. Please explain. Sorry. I am missing something very basic here.
var newobj = {'prop1' : 12, 'prop2' : 25};
myfunc(newobj);
function myfunc(someobj) {
someobj.prop3 = 45;
}
This gives a syntax error.
Chances are something else is interfering because it works for me.
If you dump newobj before the function call you get:
{"prop1":12,"prop2":25}
And after the function call:
{"prop1":12,"prop2":25,"prop3":45}
As you can see, the new property has been added.
I would suggest either looking at what you have more closesly (make sure you're not copying the value and then passing it) or add some console.log call in your code as it goes through. You can also, in most of the browsers, use the debugger to step through the code to see where it may be fouled.

Categories

Resources