0

<a id="Param">This is an html element</a>

<script>

function Append(param1,text)
{
    if(document.getElementById(param1))
    {

        return document.getElementById(param1)+text;
    }
    else
    {
        console.error("the element was not found");
    }
}

var app1=Append("Anything",". yes"); //i called the append twice first is to see the output when false
var app2=Append("Param",". hi")//second is to see the output when true. I want the output to be "This is an html element. hi"

this is the code that I have. I wanted to have a function that uses two parameters. The first one is to for an html element id and the second one is for the text that will be appended to the first parameter. How can the function check if "Param" is an id without hard coding or using jquery?

jam
  • 5
  • 3

2 Answers2

0

A string-query might be your best bet by making the PARAM dynamic and you can change it based on what the url passes to the script. index.php?param1=myvalue

Here is a reference article I used last time I had to do it myself: https://www.joezimjs.com/javascript/3-ways-to-parse-a-query-string-in-a-url/

Respectfully, SFR

Sergio Rodriguez
  • 8,258
  • 3
  • 18
  • 25
0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Lab Test 1</title>
</head>
<body>

<a id="Param">This is an html element</a>

<script>

    function appendToElement(p1,p2)
    {
        var el=document.getElementById(p1);
        if(el)
        {
            el.innerHTML+=p2;
            return true;
        }
        else
        {
        console.error("Element with ID: "+p1+" not found");
        return false;
        }
    }      

    var app1=appendToElement("Anything",". yes");
    var app2=appendToElement("Param",". hi")
</script>

</body>
</html>

This is the code that I got from my professor.

jam
  • 5
  • 3