Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
Why is this.id not working in my switch...case?

The code should show the plot for the book cover you click on whilst changing the background to an alternative cover of the book, but it isn't doing anything. I have some backup code that works, but I liked this solution more.

My js code:

function mostraTrama(){
    try{
        switch(this.id){
            case "TramaVdR":
                nodoTramaVdR.setAttribute = ("class", "TramaScelta");
                nodoTramaPdL.setAttribute = ("class", "TramaNonScelta");
                nodoTramaG.setAttribute = ("class", "TramaNonScelta");
                document.body.style.backgroundImage = "url('../Images/way_of_kings_bg.jpg')";
                break
            case "TramaPdL":
                nodoTramaVdR.setAttribute = ("class", "TramaNonScelta");
                nodoTramaPdL.setAttribute = ("class", "TramaScelta");
                nodoTramaG.setAttribute = ("class", "TramaNonScelta");
                document.body.style.backgroundImage = "url('../Images/words_of_radiance_bg.jpg')";
                break
            case "TramaG":
                nodoTramaVdR.setAttribute = ("class", "TramaNonScelta");
                nodoTramaPdL.setAttribute = ("class", "TramaNonScelta");
                nodoTramaG.setAttribute = ("class", "TramaScelta");
                document.body.style.backgroundImage = "url('../Images/oathbringer_bg.jpg')";
                break
        }
    }
    catch(e){
        alert("mostraTrama " + e);
    }
}

function gestoreLoadRomanzi(){
    try {
        nodoTramaViaDeiRe = document.getElementById("TramaViaDeiRe");
        nodoTramaParoleDiLuce = document.getElementById("TramaParoleDiLuce");
        nodoTramaGiuramento = document.getElementById("TramaGiuramento");
        nodoTramaVdR = document.getElementById("TramaVdR");
        nodoTramaPdL = document.getElementById("TramaPdL");
        nodoTramaG = document.getElementById("TramaG");
        nodoTramaViaDeiRe.onclick = mostraTrama;
        nodoTramaParoleDiLuce.onclick = mostraTrama;
        nodoTramaGiuramento.onclick = mostraTrama;
    } catch ( e ) {
        alert("gestoreLoadRomanzi " + e);
    }
}

All three start off with class = "TramaNonScelta", which has display: none, and when clicked they should change to class = "TramaScelta", which has display: inline. The IDs all match to the ones they have in the HTML document.

My alternate functioning way is to have a separate function for each book instead of a single one for all of them.

EDIT: I also have a very similar code for another program that instead of using .setAttribute uses .innerHTML and works without any problems (but my professor doesn't want us to use .innerHTML in this project)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!