Overwrite paragraph button.onclick()

Overwrite paragraph button.onclick()



How can I prevent this from happening when i press the button?



Instead of printing below the other output above, I want it to replace what is already there.



First click:





Second click:





Code:




var sq = "";
var outSquare = document.getElementById("outSquare");
var inSquare = document.getElementById("inSquare");
var bSquare = document.getElementById("bSquare");


bSquare.onclick = function ()
var g = inSquare.value;
outSquare.innerHTML = square(g);


function square(a)
for (var i = 1; i <= a; i++ )

for (var j = 1; j <= a; j++)
sq += "*";


sq += "<br>";

return sq;


<p id="outSquare"></p>
<input id="inSquare" type="number"/>
<button id="bSquare" type="button"> Print </button>






Move var sq = ""; to inside the function square - Voting to close as This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers.

– mplungjan
Sep 18 '18 at 13:17



var sq = "";


square




2 Answers
2



You need to initialise sq inside the function too:


sq




var sq = "";
var outSquare = document.getElementById("outSquare");
var inSquare = document.getElementById("inSquare");
var bSquare = document.getElementById("bSquare");


bSquare.onclick = function ()
var g = inSquare.value;
outSquare.innerHTML = square(g);


function square(a)

sq = ""; // empty this string on click

for (var i = 1; i <= a; i++ )

for (var j = 1; j <= a; j++)
sq += "*";


sq += "<br>";

return sq;


<p id="outSquare"></p>
<input id="inSquare" type="number"/>
<button id="bSquare" type="button"> Print </button>






in square function added stmt sq = "";

– Aagam Jain
Sep 18 '18 at 13:16






Sorry I didn't get you. OP means?

– Aagam Jain
Sep 18 '18 at 13:20



You are missing sq = ''; inside the function square(a). Doing sq="" will re-initialize the value of sq to empty for new iteration and will output the desired behaviour.


sq = '';


square(a)


sq=""


sq




var sq = "";
var outSquare = document.getElementById("outSquare");
var inSquare = document.getElementById("inSquare");
var bSquare = document.getElementById("bSquare");


bSquare.onclick = function()
var g = inSquare.value;
outSquare.innerHTML = square(g);


function square(a)
sq = '';
for (var i = 1; i <= a; i++)
for (var j = 1; j <= a; j++)
sq += "*";

sq += "<br>";

return sq;


<p id="outSquare"></p>
<input id="inSquare" type="number" />
<button id="bSquare" type="button"> Print </button>






Identical to stackoverflow.com/a/52387474/295783 and not worth answering

– mplungjan
Sep 18 '18 at 13:16







@mplungjan I did not answer this after 10-20 mins -1 day after it was answered.

– Ankit Agarwal
Sep 18 '18 at 13:32







But you are getting the accept - you could have gracefully deleted your answer when you saw the lower repped Aagam had answered before you....

– mplungjan
Sep 18 '18 at 13:35






@mplungjan I did not see that. Sorry. I will make sure I see such answers in future. Thanks for your suggestion

– Ankit Agarwal
Sep 18 '18 at 13:36



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ