Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 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
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
+ 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.
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)
0
as you get value as string you should convert it to number to avoid string concanetation:
Number(string_value)+1
0
Hi @visph ,
Thanx visph. Actually, I knew about innerHTML but didn't knew how to use. Thanx for informing me.
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 ;)
0
say you want assign to paragraph a string with a line break:
document.getElementById("def").innerHTML = "Hello<br>World";
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?
0
search in Q&A: this question has been posted a lot of time ^^
0
How to add if or while statements in these cases ?
0
what case?
0
Hi Harsh Kishore
Just added a parseInt to convert the value of x to an integer. It should work now
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 ?
0
Ollie Q , I didn't understood parselnt. Can you explain and show ?
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
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";
}
0
Ollie Q
https://code.sololearn.com/Ww1BWoYg9lda/?ref=app
Its not working
0
document.getElementById("abc").innerHTML = parseInt(x) + 1;
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 ;)