2

I'm trying to add multiple classes from JSON file to a HTML element that I created with Jquery in the same function. Problem is none of the classes are going trough. Some of the JSON data sections(items) have more than one class, they are set in an array. HTML part is created and it gets the dat.location but its not getting any of the classes. I think there is something wrong in how I'm inserting the classes but not sure what.

How can I insert the classes to txtp variable in the function?

function CreateContent(data) {
    for (var i = 0; i < data.length; i++) {
        var dat = data[i];
        var txtp = $('<p class="column" href="#">' + dat.location + '</p>');
        $(txtp).addClass(dat.items1);
        $(txtp).addClass(dat.items2);
        $(txtp).addClass(dat.items3);
        $(".positionInHtml").append(txtp); 
    }
}

This is what the JSON data file contains, thou I simplified/changed the variable names for this question.

{
  "Places": [
    {
      "location": "Name1",
      "items1": [ "1A" ],
      "items2": [ "1B","11B" ],
      "items3": [ "1C" ]
    },
    {
      "location": "Name2",
      "items1": [ "2A" ],
      "items2": [ "2B" ],
      "items3": [ "2C","22C" ]
    }
}
3
  • 2
    Can you show us dat structure and preferably a working snippet demonstrating your issue? Commented Feb 7, 2018 at 8:38
  • Actually there is no $(txtp) object. Commented Feb 7, 2018 at 8:41
  • I added the data content from JSON file. Commented Feb 7, 2018 at 8:59

3 Answers 3

1

Your problem is because you are creating new jQuery objects from txtp every time you use it, so none of the changes are retained.

To solve this create the object once, then amend/append it as required:

function CreateContent(data) {
  for (var i = 0; i < data.length; i++) {
    var dat = data[i];
    var $txtp = $('<p class="column" href="#">' + dat.location + '</p>').appendTo('.positionInHtml');
    $txtp.addClass(dat.items1);
    $txtp.addClass(dat.items2);
    $txtp.addClass(dat.items3);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This seemed like it would work and made sense but I think something with the JSON file data arrays didn't work with it. I added the JSON contents in the question.
That's because some of the properties are arrays, not strings, so you will need another loop to go through them and add the class names.
0

Try This

function CreateContent(data) {
    for (var i = 0; i < data.length; i++) {
        var dat = data[i];
        var classes = dat.items1+' '+dat.items2+' '+dat.items3;
        var txtp = $('<p class="column "'+classes+' href="#">' + dat.location + '</p>');
        $(".positionInHtml").append(txtp); 
    }
}

1 Comment

Some explanation on why he should try this and why this works would be nice.
0

The problem seemed to be that in the JSON file the using arrays to store the classes then trying to insert them with Jquery .addClass it does not give the values to the target in this case txtp. Once i removed arrays ["A1","A2"] and made them in string "A1 A2" it worked as intended and the classes were given to txtp.

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.