HTML and JavaScript: Change button text (Toggle)

HTML and JavaScript: Change button text (Toggle)



I need to change a button's text, however I think the issue I don't understand is how to change it back using separate files. I need to use jQuery to toggle pictures. This will hide the image and show the image. The button is "hard coded" I think in both the HTML and JavaScript.



The button is showing "hide" to initially hide the image. Once the button is clicked the image disappears and the button's text turns to "Show". However it will not turn back to "hide".



HTML:


<img src="sky.jpg" id="sky">
<input type='button' onclick="js/toggle.js" id="skybutton" value="Hide">

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/toggle.js"></script>



JavaScript:


$('#skybutton').click(function()
$("#sky").toggle();
$(skybutton).val('Show');
);






have you tried adding a conditional to check the name of the label ?

– Julian Silvestri
Sep 16 '18 at 23:48






you dont need onclick in html, just connect script to html on end of body tag

– Mladen Skrbic
Sep 17 '18 at 5:23




3 Answers
3



Welcome to stackoverflow Shepard!!



You need little bit of logic in your javascript in order to make this work both ways:


<img src="sky.jpg" id="sky">
<input type='button' onclick="js/toggle.js" id="skybutton" value="Hide">



Here in the click function is where you need magic to happen:
Try this code:


$('#skybutton').click(function()
$("#sky").toggle();
if($(this).val() === 'Hide')
$(this).val('Show');
else
$(this).val('Hide');

);



So what is happening above is that you already have a click function attached to the button so with in the function you can refer to that button as this and you can check what the value of the button is.


this



Your if statement goes hey button if your value is 'Hide' change the value to 'Show' and else your value must be 'Show' so change it back to 'Hide' let me know if I can clear anything else up for you. Good luck with the project


if



You can achieve this using an if else statement say:


$('#skybutton').click(function()
If($(this).val() == "hide")
$("#sky").hide();
$(this).val("Show")
else
$("#sky").show();
$(this).val("hide")


);






Was using my phone and barely noticed some syntax typo error the val is in small letters not capita letters

– user8453321
Sep 16 '18 at 23:55



In support of I am Cavic, just a more readable version


$('#skybutton').click(() =>
$("#sky").toggle()
if ($(this).val() === "hide")
$(this).val("show")
return

$(this).val("hide")
)






thank you for the support have ever your code most likely won't work for the poster he will need to use es6 processor for => notation which by his original post you can tell he is not using also your last line `.Val("hide") will throw an error

– I am Cavic
Sep 17 '18 at 0:28






'readable' is a matter of opinion, I prefer @I am Cavic's solution.

– Poul Bak
Sep 17 '18 at 4:49






@I am Cavic, ya sure the Val() was a typo. Support for the feature is pretty good, a 86.68% of browsers supports it source : caniuse.com. @Poul Bak, yes you're totally right readability is a opinion and it is something we as developers should promote.

– wokoro douye samuel
Sep 17 '18 at 4:53



Val()



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?

Node.js puppeteer - Use values from array in a loop to cycle through pages