Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is the code I’ve which should change to “hide details” when I click on “show details”:

out.print("<a href=\"javascript:parent.showHideDetails('"+id+"',this);\">show details</a>");

function showHideDetails(id,obj) {

    try

    {

        alert(obj.innerText);

        obj.innerText = 'hide details';

    }

    catch (err)

    {

        alert(err);

    }

}

But it’s not working properly. Can anyone tell me what I’m doing wrong here?

1 Answer

0 votes
by (19.7k points)

You should point the href attribute to the anchor element with the same ID. Then you can use the document.getElementById to get the anchor like below: 

out.print("<a href=\"javascript:parent.showHideDetails('"+id+"','anchor_1');\" id=\"anchor_1\">show details</a>");

function showHideDetails(id,identifier) {

    try

    {

        var obj = document.getElementById(identifier);

        alert(obj.innerText);

        obj.innerText = 'hide details';

    }

    catch (err)

    {

        alert(err);

    }

}

Interested in Java? Check out this Java tutorial by Intellipaat.   

Related questions

0 votes
1 answer
asked Apr 7, 2021 in Java by dante07 (13.1k points)
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
asked May 11, 2021 in Java by sheela_singh (9.5k points)

Browse Categories

...