user3328590
user3328590

Reputation: 11

Trying to populate <h1> text with Javascript For In loop

Im trying to get the to populate with companyName from an array. I am not seeing any changes.

Javascript:

var webName = {
coinstar: {
    companyName: "Coinstar",
    projectName: "Online App",
    role: "Web Developer",
    detailText: "This is some text"
    },
google: {
    companyName: "Google",
    projectName: "AdWord Security",
    role: "Web Developer",
    detailText: "This is some text"
    },
amazon: {
    companyName: "Amazon",
    projectName: "Internal Site",
    role: "Web Developer",
    detailText: "This is some text"
    }
}

// The is supposed to grab the company name from the above list. 
var search = function(coName){
for(var prop in coName){
    if(webName[prop].companyName === coName){
        document.getElementById("companyName").innerHTML = companyName;
        }
    }
}

var detailTitle = function(){
search("coinstar");

HTML: // Click this div to populate

// After div is pressed this heading should say "Coinstar".
    <h1 id="companyName">Company name here</h1>

Upvotes: 0

Views: 83

Answers (4)

vijay
vijay

Reputation: 631

try this.

var search = function(coName){
 for(var prop in webName){
    if(webName[prop].companyName === coName){
    document.getElementById("companyName").innerHTML = webName[prop].companyName;
    }
 }
}

Upvotes: 0

Matt Burland
Matt Burland

Reputation: 45155

It doesn't make sense to do for...in on the string you pass to search. You should be able to just do this:

var search = function(coName){
    if(webName[coName]){
        document.getElementById("companyName").innerHTML = webName[coName].companyName;
    }
}

If, however, the idea was that the key in your webName object and the term passed to search aren't the same, then you can for...in over webName and try to match .companyName (note, however, that string comparisons are case sensitive)

var search = function(coName) {
    for(var prop in webName) {
        if(webName.hasOwnProperty(prop) && webName[prop].companyName && webName[prop].companyName.toLowerCase() === coName.toLowerCase()) {
            document.getElementById("companyName").innerHTML = webName[prop].companyName;
        } 
    }
}

Upvotes: 0

Mike Christensen
Mike Christensen

Reputation: 91666

For now, I'll ignore the fact that id="companyName> is missing an ending quote.

In the function:

var search = function(coName) {
   for(var prop in coName) {

coName is the string coinstar:

search("coinstar");

You're attempting to iterate through each property in a string, which is probably not what you want.

Perhaps you meant to iterate through your collection of companies:

var search = function(coName) {
   for(var prop in webName) {

With that said, there's not really a reason to iterate through the collection to find your company name, when you already key the collection by company name. webName[coName] should get you what you want without the for loop.

Upvotes: 1

Random
Random

Reputation: 467

<div id="companyDiv">
</div>

In javascript

document.getElementById("companyDiv").innerHTML+="<h1>"+companyName+"</h1>"

Upvotes: 0

Related Questions