Javascript code to change text on button click, which can return the old text | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Javascript code to change text on button click, which can return the old text

Hello! I want to create a website where you can change the language. The fact is that on modern sites this happens due to the link when you click on another language. Is it possible to change one text to another by pressing a button in JavaScript?

24th Aug 2023, 5:46 AM
Sliskom Umniy
Sliskom Umniy - avatar
7 Answers
+ 6
Can you share your attempt?
24th Aug 2023, 6:04 AM
Sakshi
Sakshi - avatar
+ 1
Yes it is posible.Share some of your code and.
24th Aug 2023, 8:33 AM
Samir Krasnic
Samir Krasnic - avatar
+ 1
you have few errors here,i mean many 1.first you need to load DOM before you find buttons with IDs: Add in top ; document.addEventListener("DOMContentLoaded", function() { *dont forget to close with { on the end 2.you have semicolon on line 4 and you dont need it. 3.Close your functions and if and else stqtements corectly 4.and yea I almost forgot,you use === to check if value is identical not = **If you still dont menage to do it i can give you more hints after yoi fix all above you will see that you also have IDs problem
24th Aug 2023, 6:40 PM
Samir Krasnic
Samir Krasnic - avatar
+ 1
yea ok,but since you have only two languahes here you dont need if/else statement at all.You can simplyswitch from one to another language using bolean value atached to your button.
24th Aug 2023, 9:48 PM
Samir Krasnic
Samir Krasnic - avatar
+ 1
it's absolutely possible to create a website where users can change the language by pressing a button using JavaScript. You can achieve this by defining different sets of text for each language and then updating the displayed text based on the user's language choice.
25th Aug 2023, 8:25 PM
Jason Jaxon Twange Lufungulo
Jason Jaxon Twange Lufungulo - avatar
0
I think that changing the language without changing the site can only happen according to this scenario: We have a button. When clicked, it changes its value. If its value is zero, then there is one language. If the value is equal to one, then changes to another language, and if the value is two, then the value is reset. I have something wrong with the 4th line, while I can't figure out what exactly https://code.sololearn.com/W6LN5Zr5dQ01/?ref=app
24th Aug 2023, 6:19 PM
Sliskom Umniy
Sliskom Umniy - avatar
0
Thanks to everyone who answered my question! I optimized the code a bit, when did I find out better about textContent, and the output came out: https://code.sololearn.com/WGFfwZAno25k/?ref=app
24th Aug 2023, 9:36 PM
Sliskom Umniy
Sliskom Umniy - avatar