Why ghe value is not changing ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why ghe value is not changing ?

I want to change value in this code in p tag to the value written in input tag. What is going wrong ? https://code.sololearn.com/WheMqexK9z4o/?ref=app

6th Jun 2021, 3:30 AM
Harsh
Harsh - avatar
29 Answers
+ 2
Hi Harsh Kishore Here is your fixed code https://code.sololearn.com/Wf1dFmjL1WXg/?ref=app 2 things: 1 .value is used to get value from input tags 2. .textContent = is used to set value of text in p tags Also did you want to add one to the value of #abc? Because i added that to
6th Jun 2021, 3:37 AM
Ollie Q
Ollie Q - avatar
+ 1
Hi Ollie Q , Thank you very much, I cant tell how much I have got happiness to see my problem solved but, I wanted to add +1 means for example x = 1 result should be 2 like this. If you can resolve it will be better and also add comment where you have used #abc.
6th Jun 2021, 3:56 AM
Harsh
Harsh - avatar
0
p elements have no 'value' properties (only input/form fields have one)... for most of elements you should assign the 'textContent' property ;) (or innerHTML if you want to replace html code)
6th Jun 2021, 3:37 AM
visph
visph - avatar
0
as you get value as string you should convert it to number to avoid string concanetation: Number(string_value)+1
6th Jun 2021, 3:59 AM
visph
visph - avatar
0
Hi @visph , Thanx visph. Actually, I knew about innerHTML but didn't knew how to use. Thanx for informing me.
6th Jun 2021, 4:08 AM
Harsh
Harsh - avatar
0
as you use value or textContent: by assigning an html string wich will be parsed on the fly to update the page/element content ;)
6th Jun 2021, 4:09 AM
visph
visph - avatar
0
say you want assign to paragraph a string with a line break: document.getElementById("def").innerHTML = "Hello<br>World";
6th Jun 2021, 4:11 AM
visph
visph - avatar
0
I have a problem I have activated my account in email but still it's saying your account is not activated, Check you mail. Any solution?
6th Jun 2021, 4:17 AM
Harsh
Harsh - avatar
0
search in Q&A: this question has been posted a lot of time ^^
6th Jun 2021, 4:18 AM
visph
visph - avatar
0
How to add if or while statements in these cases ?
6th Jun 2021, 4:20 AM
Harsh
Harsh - avatar
0
what case?
6th Jun 2021, 4:21 AM
visph
visph - avatar
0
Hi Harsh Kishore Just added a parseInt to convert the value of x to an integer. It should work now
6th Jun 2021, 4:23 AM
Ollie Q
Ollie Q - avatar
0
visph , these cases means document.getelementbyid case. In this, I want to add if statement for example if x=1, y =2. (Just for example, not related to the code above). Can you give example ?
6th Jun 2021, 4:24 AM
Harsh
Harsh - avatar
0
Ollie Q , I didn't understood parselnt. Can you explain and show ?
6th Jun 2021, 4:26 AM
Harsh
Harsh - avatar
0
parseInt() converts the value in the parentheses to an integer if possible. So if you had the string “43” and passed it into the parseInt() function it would convert it from a string to an integer and allow you to use mathematical operations such as + or - on it Also #abc refers to the input tag as it’s id is abc and the # is used to reprent id in css
6th Jun 2021, 4:29 AM
Ollie Q
Ollie Q - avatar
0
Harsh Kishore first you must distinguish between assignement operator (=) and comparison operator (==)... then you make an if..else statement as: if (x==1) { htmlElementReference.textContent = "true"; } else { htmlElementReference.textContent = "false"; }
6th Jun 2021, 4:33 AM
visph
visph - avatar
6th Jun 2021, 4:37 AM
Harsh
Harsh - avatar
0
document.getElementById("abc").innerHTML = parseInt(x) + 1;
6th Jun 2021, 4:39 AM
visph
visph - avatar
0
but in that case, you doesn't need to parse value as html: better to use textContent... also, using Number() allow int or float... there's also a parseFloat() function ;)
6th Jun 2021, 4:41 AM
visph
visph - avatar