Jarrett Horton Jarrett Horton - 23 days ago 8
Javascript Question

Dynamic link that parses to the element I type

my goal is to type a stock symbol in the search bar and have a dynamic way for a link to be attached to the result. For example, when I search for FLWS, a element appears, like the one below, and when I click on that element it takes me to a specific page. So when I type FLWS in the search bar it allows me to go to this url: https://api.iextrading.com/1.0/stock/FLWS/quote and when I type MSFT it allows me to go to https://api.iextrading.com/1.0/stock/MSFT/quote

Thank you in advance.

enter image description here

My jsFiddle: http://jsfiddle.net/9afw1wq9

So my question is how do I make a dynamic link that parses to the element I type. I have this so far:

<!DOCTYPE html>
<html>
<head>
<!-- inserting jquery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<style>
* {
box-sizing: border-box;
}

#myInput {
//background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px; /* Prevent double borders */
background-color: #f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block
}

#myUL li a:hover:not(.header) {
background-color: #eee;
}
</style>
</head>
<body>
<h2>My Portfolio</h2>
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for a Stock Symbol.." title="Type in a name">
<ul id="myUL">
<li><a href="#">Christina</a></li>
<li><a href="#">Cindy</a></li>

<!-- Starting the Stock Test -->
<li><a href="#">FLWS</a></li>
</ul>
<script>

function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
console.log(input);
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";

}
}
//Returning the result
console.log(filter);

var parser = document.createElement('a');
//End result should look like: https://api.iextrading.com/1.0/stock/VNOM/quote
parser.href = "https://api.iextrading.com/1.0/stock/";

parser.StockName = filter;
console.log("Parser StockName: " + parser.StockName);

var url = parser.href;
var stockName = parser.StockName;
var result = url + stockName;

//concat quote to the end of the url
var quoteAdd = "/quote";
result = result + quoteAdd
console.log("quote: " + quoteAdd);
console.log("Final url: " + result);

//counting letters in the filter
var count = filter.replace(/[^A-Z]/gi, "").length;
console.log(count);
if (count == 2 || count == 3 || count == 4 && filter){
if($("#myUL").length) {
console.log("nested if result: " + result);

/* I need to find the element that i searched for, then add the result url to the button.
For example, when i search for FLWS, i click on that tag to take me to a new page.
*/

ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
console.log(li);

var result1 = document.getElementById('li');
console.log("nested result After: " + result);
console.log("result1: " + result1);
}
}
return result;
}
</script>
</body>
</html>

sbr sbr
Answer Source

Your code is functional till the url construction. You just need to add the result to the document. see the last two lines.

if (count == 2 || count == 3 || count == 4 && filter){
    if($("#myUL").length) {
        console.log("nested if result: " + result);

        /*  I need to find the element that i searched for, then add the result url to the button.
            For example, when i search for FLWS, i click on that tag to take me to a new page.
         */

        ul = document.getElementById("myUL");
        li = ul.getElementsByTagName("li");
        console.log(li);

        var result1 = document.getElementById('li');
        console.log("nested result After: " + result);
        console.log("result1: " + result1);
        var resultDiv = "<li><a href=" + "'" + result + "'" + ">" + filter + "</a></li>"
        $("#myUL").append(resultDiv);
    }
}