Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72358

How do I make this JavaScript code show up in HTML? [closed]

$
0
0

I have a code with this structure that I have to show in HTML. I've run the code in the console and it works, I just can't seem to make it show up in my HTML.

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

var nameKey = document.getElementById("search").value;
function search(nameKey, array){
    for (var i=0; i < array.length; i++) {
        if (array[i].name === nameKey) {
            return array[i];
        }
    }
}
document.getElementById("demo").innerHTML = nameKey;

This is my HTML code:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Names</title>

</head>
<body>
<h1> Name in array </h1>

    <input type="text" name="text" id="search" />   
    <input type="submit" value="Search" onclick="search(nameKey,array)" />
    <p id="demo"></p>

    <script type="text/javascript" src="script.js"></script>
</body>

</html>

I want to have the array show up in the HTML. As if I search for "string 1" it makes the object that contains that name appear.


Viewing all articles
Browse latest Browse all 72358

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>