I am a beginner in javascript and I have this code
This code shows the user to add new input
but I think I can make it shorter but I can’t find the right way?
showCurs.onclick = function() {
curses.style.display="block";
}
hideCurs.onclick = function() {
curses.style.display="none";
}
showaddcurs.onclick = function() {
addc.style.display="block";
}
hideaddcurs.onclick= function() {
addc.style.display="none";}
showExp.onclick = function() {
exp.style.display="block";
}
showaddexp.onclick = function() {
adde.style.display="block";
}
hidexp.onclick = function() {
exp.style.display="none";
}
2
Answers
Add a data attribute to each button indicating the ID (or some other unique attribute) of the element to be toggled. Use another attribute or the text content of the button to indicate whether the element should be shown or hidden (if the buttons are always visible and you don’t already have a way to distinguish between them).
So for
showCurs
,hideCurs
, andcurses
, you could haveFurther modifications only require adding in the HTML for the show/hide buttons and their associated toggle target.
There are lots of ways to change your code to help eliminate duplicate parts. For example: