1

I have the following object:

{
    builders: [
        {
            name: "Branches > Build",
            slaves: [
                "slave-01.local"
            ]
        },
        {
            name: "Branches > Build1",
            slaves: [
                "slave-02.local"
            ]
        }
    ]
}

I want to update the html table td if their parent tr id matches the name attribute of the json.

<table>
  <tr id="Branches > Build" class="odd">
    <td>
      value of slaves of Branches > Build should be here
    </td>
  <tr id="Branches > Build1" class="even">
    value of slaves of Branches > Build1 should be here
    <td>
    </td>
<table>

How do I achieve this? I'm using jquery.

Btw. I can't get id's with no spaces so it must work with that.

5
  • 4
    You id is invalid change it as id="Branches_Build1" from id="Branches > Build1" Commented Jan 20, 2014 at 15:07
  • I know but I'm in a situation where i can't get that right now. Commented Jan 20, 2014 at 15:08
  • Your JSON is not valid JSON. Are you use it is JSON and not just a JavaScript object? Commented Jan 20, 2014 at 15:09
  • 1
    What are you stuck on here? Looping over the array? Finding the elements? Updating the text? Commented Jan 20, 2014 at 15:11
  • Demo: jsfiddle.net/UaLNy, this is the one you have expected? Commented Jan 20, 2014 at 15:14

3 Answers 3

1

If you can't change the id to a valid id, you can use Attribute-selectors [id=""]

var obj = "the jsonfile";
var objBuilder = obj.builders;
for (var key in obj) {
    var item = $('[id="' + obj[key].name + '"]')
    if (item.length) {
        item.find('td').text(obj[key].slaves);
    }
}

DEMO fixed html markup aswell

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

Comments

0

Can you try this,

    var json = {
        builders: [
            {
                name: "Branches > Build",
                slaves: [
                    "slave-01.local"
                ]
            },
            {
                name: "Branches > Build1",
                slaves: [
                    "slave-02.local"
                ]
            }
        ]
    }

    $("table tr").each(function(index, obj){

        console.log(index+obj.id + json.builders[0].name);

        if(obj.id == json.builders[0].name){

            $(this).find('td').html('test');
        }

    });

Comments

0

Please refer to basic types

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number
 of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods   (".").

IDREF and IDREFS are references to ID tokens defined by other attributes. IDREF is a single token and IDREFS is a space-separated list of tokens.

NUMBER tokens must contain at least one digit ([0-9]).

So you probably have to find a better way to solve it and use proper IDs. I suggest you to go for javascript frameworks such as "Aungular" that will make your job simpler and you can do much more things using it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.