Lines 12-22
Function openManu currently displays a block on load and makes a for loop for my array. What this does is then creates an obj variable based on the array and takes Manufacturer from the array that's defined by the links that calls the function. So a link in my HTML for instance will have an onMouseOver event tied to openManu('manufacturer') and manu will be defined as manufacturer, only the "Description" in the array corresponding to this manufacturer will be inserted into the HTML.
My problem is I'm trying to create another function that runs before this that goes through my array and creates the links depending on what's in the array. Then since this gets called once the body loads the links should be present and the links will also have the onMouseOver event where the second function from lines 12-22 can be called.
Currently only openManu() works onMouseOver and will insert the Description based on the specified manufacturer into an object in my HTML called content. The createLinks() function I have opens the array and defines URL as a variable and inserts it into a link tag that gets created.
Is this possible to do? And is the order messing it up?
javascript:
var arr =
[
{
"Manufacturer": "Any manufacturer",
"Description": "Traditional values",
"URL": "http://www.website.com"
},
{
"Manufacturer": "Different Manufacturer",
"Description": "Short description of said manufacturer",
"URL": "http://www.website2.com"
},
{
"Manufacturer": "A Similar Manufacturer",
"Description": "Not quite the same as the previous manufacturer",
"URL": "http://www.website3.com"
},
{
"Manufacturer": "Manufacturer",
"Description": "Essentially a ripoff of the first manufacturer",
"URL": "http://www.website4.com"
}
]
function createLinks() {
for (var i=0; i<arr.length; i++){
var obj = arr[i];
var m = obj["Manufacturer"];
if (manu == m) {
URL = obj["URL"];
}
}
document.getElementById('col').innerHTML = "<a onmouseover=\"openManu(\'" ++ m ++ "\')\" onmouseout=\"mouseOut()\" onClick=\"openWeb(\'" ++ URL ++ "\')\">" ++ m ++ "</a>");
}
function openManu(manu) {
document.getElementById('content').style.display = 'block';
for (var i=0; i<arr.length; i++){
var obj = arr[i];
var m = obj["Manufacturer"];
if (manu == m) {
desc = obj["Description"];
}
}
document.getElementById('content').innerHTML = desc;
}
window.onmousemove = function (e) {
var tooltipSpan = document.getElementById('content');
var x = e.clientX,
y = e.clientY;
tooltipSpan.style.top = (y - 20) + 'px';
tooltipSpan.style.left = x + 'px';
}
var mouseOut = function(){
document.getElementById('content').style.display = 'none'
}
function openWeb(URL) {
window.open(URL);
}
#container{
width:870px;
margin-top:2em;
font-size:1.1em;
position:relative;
padding-left:20px;
display:inline-block;
background-color:#3C3C4E;}
#content{
z-index:1;
display:none;
width:300px;
font-size:16px;
font-family: 'Raleway', sans-serif;
position:absolute;
padding:10px;
background-color:white;}
a {
cursor:pointer;
padding:0;
display:inline-block;
margin:0;
color: white;
font-family: 'Raleway', sans-serif;
position:inherit;
}
h4 {
padding:0;
z-index:0;
display:inline-block;
margin:0;
color: white;
font-family: 'Raleway', sans-serif;
font-weight:normal;
font-size:15px;
background-color:#3C3C4E;
position:absolute;
left:8px;
padding:24px;
top:400px;
width:842px;
}
pre {
display:block;
float:left;
line-height:21px;
}
<!DOCTYPE html>
<html onload="createLinks()">
<div id="content"></div>
<pre id="col"></pre>
</html>
The old HTML included links that looked like this.
<a onmouseover="openManu('Any manufacturer')" onmouseout="mouseOut()" onClick="openWeb('http://www.website.com/')">Any manufacturer</a>
@Zer00ne in response to your answer, I changed createLinks() to this. I can't get it to work I might not fully understand your solution.
function createLinks() {
arr[i]["Manufacturer"]
var obj = arr[i];
var m = obj["Manufacturer"];
document.getElementById('col').innerHTML = "<a onmouseover=\"openManu(\'" ++ m ++ "\')\" onmouseout=\"mouseOut()\" onClick=\"openWeb(\'" ++ URL ++ "\')\">" ++ m ++ "</a>");
}
arr[i]["Manufacturer"]thanvar obj = arr[i]; var m = obj["Manufacturer"];?innerHTMLmakes kitten cry. Why notcreateElement('a')and attach the event directly in the JavaScript. Using in code function references is far more performant and easier to reason then having the browser parse HTML andeval()strings which will introduce XSS (cross site scripting) problems.createLinks()but now there another series of erreors...innerHTMLdo in fact make kittens cry. I would've went the way ofcreateElement('a'), but I'm running out of time.