0

I have a simple object created through JQuery:

var userSettings = {
    firstName: "John",
    lastName: "Smith",
    username: "Jsmith"
};

The HTML is quite simple as well:

<p>First Name <span id="firstName"></span></p>
<p>Last Name <span id="lastName"></span></p>
<p>Username <span id="username"></span></p>

My question is, how do I write a simple function that will support adding more key/values to the object. For this example, the function would simply update the text of the corresponding span with the proper ID.

Additionally

Parts of the object will not necessarily going to be handled through .text - is there a way to filter/define what method to use based off of something defined in the object? For example adding in userColor: red would use .css rather than .text.

JS Fiddle Link

Edit: Updated Fiddle with Function

6
  • 3
    1. This code does nothing. What is it supposed to do, and what have you tried? 2. This has nothing to do with jQuery (at least so far) Commented Jun 5, 2015 at 21:04
  • Oh, whoops - I forgot to include my function - jsfiddle.net/bce7ubqx/2 - I'm not sure how to handle condensing it to run more effeciently with more parts of the object. Commented Jun 5, 2015 at 21:08
  • Please update the question as well. It helps to have all relevant information inside the question itself. Commented Jun 5, 2015 at 21:09
  • For that you need to chain jQuery. See Fiddle here Commented Jun 5, 2015 at 21:28
  • Maybe I explained wrong - but my point is that certain key/values would use a different method. So rather than using .text certain key/values would use .css Commented Jun 5, 2015 at 21:31

5 Answers 5

3

Could be:

for (var key in userSettings) {
  if (userSettings.hasOwnProperty(key)) {
    $('#' + key).text(userSettings[key]);
  }
}

FIDDLE: https://jsfiddle.net/lmgonzalves/bce7ubqx/1/

EDIT:

A possible solution (include your styles in object):

var userSettings = {
    firstName: "John",
    lastName: "Smith",
    username: "Jsmith",
    styles: {
        color: "red",
        backgroundColor: "gray"
    }
};

for (var key in userSettings) {
    if (userSettings.hasOwnProperty(key)) {
        if(key !== 'styles'){
            $('#' + key).text(userSettings[key]);
            if (userSettings.hasOwnProperty('styles')) {
                var styles = userSettings['styles'];
                for (var k in styles) {
                    if (styles.hasOwnProperty(k)) {
                        $('#' + key).css(k, styles[k]);
                    }
                }            
            }
        }
    }
}

FIDDLE: https://jsfiddle.net/lmgonzalves/bce7ubqx/5/

Sign up to request clarification or add additional context in comments.

1 Comment

If the object contained settings that wouldn't be updated via .text, is there a way to differentiate this in the loop? For example, adding userColor: red in the object, but wanted this object to run with a .css method, how could I edit the loop to work based off of that?
1

This will upadate the span text according to your userSettings

$(document).ready(function(){
    $.each(userSettings,function(k,v){
        if($('#'+k).length)
            $('#'+k).text(v);
    })
});

Edit:

If you want to differentiate between id and style, add flags in loop

var userSettings = {
    firstName: "John",
    lastName: "Smith",
    username: "Jsmith",
    styles: {
        color: "red"
    }
};

$(document).ready(function(){
    $.each(userSettings,function(k,v){
        if($('#'+k).length)
        {
            $('#'+k).text(v);
        }
        else if(k=='styles')
        {
            $('any_selector_here').css(userSettings.styles);
        }
    })
});

See working Fiddle here

Comments

0

Try this

function update(){
    $("#firstName").text(userSettings.firstName);
    $("#lastName").text(userSettings.lastName);
    $("#username").text(userSettings.username);
}

Comments

0

This is simple enough to achieve without jQuery at all:

var userSettings = {
    firstName: "John",
    lastName: "Smith",
    username: "Jsmith"
};

function setElementText(id, text) {
    document.getElementById(id).innerHTML = text;
}

window.addEventListener('load', function() {
    Object.keys(userSettings).forEach(function(key) {
        setElementText(key, userSettings[key]);
    });
});
<p><strong>First Name</strong> <span id="firstName"></span></p>
<p><strong>Last Name</strong> <span id="lastName"></span></p>
<p><strong>Username</strong> <span id="username"></span></p>

Also on JSFiddle

Of course, if you're trying to learn jQuery, the other answers are valid.

2 Comments

Thank you. Not sure if you saw my recent edit, about handling more than the .text (or in with JS setElementText). This is a reason I'd prefer to use jQuery. Using something like .css would help greatly.
This can be as elaborate as you need it to be... your userSettings object can hold internal objects, not just strings. Use this data to direct values to the proper HTML section (content, class, style...)
0

Add this to your script

var count = 0;
   $.each(userSettings, function(i, value) {
   //alert(count);
      $("p span:eq("+count+")").attr("id", value)
      count++;    
});

You can also add css attributes in this technique.

http://jsfiddle.net/bce7ubqx/7/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.