2

JSON {"benefits": [ { "title": "Free", "description": "Members receive free shipping" }, { "title": "Delivery", "description": "Your ride will always be in delivered" }, { "title": "News", "description": "A Bi-Weekly newsletter" } ] }

JAVASCRIPT

var benefitsVar = document.getElementById("memberBenefits");
or (var l=0; l < dataPull.benefits.length; l++){

benefitsVar[l].getElementsByTagName('h4')[0].innerHTML = 
dataPull.benefits[l].title;
benefitsVar[l].getElementsByTagName('p')[0].innerHTML = 
dataPull.benefits[l].description;

};

HTML

<section id="memberBenefits">

<ul>
  <li > <h4 class="benefits">Lorem Ipsum</h4></li>
 <li> <p class="benefits">Lorem ipsum dolor sit amet, consectetur 
adipiscing elit, sed do eiusmod tempor </p></li>
</ul>
<ul>
 <li> <h4 class="benefits">Lorem Ipsum</h4></li>
<li><p class="benefits">Lorem ipsum dolor sit amet, consectetur 
adipiscing elit, sed do eiusmod tempor </p></li>
</ul>

<ul>
    <li> <h4 class="benefits">Lorem Ipsum</h4></li>
    <li ><p class="benefits">Lorem ipsum dolor sit amet, consectetur 
adipiscing elit, sed do eiusmod tempor </p></li>

</ul></section>
1
  • Have posted answer helps you? If not then expected output? Commented Dec 2, 2018 at 6:34

1 Answer 1

1

Here's how you do it using ajax :

Note : I don't understand why you don't want to use jquery.It won't harm if you use jquery. Use jquery for the ajax thing and use javascript for the other things :

$.ajax({
    url: 'http://localhost/data.json',
    crossDomain:true,
    dataType: 'json',             
    success: function(data){
    var newData = '';
    var i;
    for(i=0; i<benefits.length; i++){
     newData +='<ul>'+
    '<li > <h4 class="benefits">'+benefits[i].title+'</h4></li>'+
    '<li ><p class="benefits">'+benefits[i].description+' </p></li>'+
    '</ul>';

    }
$('#memberBenefits').html(newData);
    },
    error: function(){
        console.log('Could not get Data from the file');
    }
});   

Your html :

<section id="memberBenefits">


</section>
Sign up to request clarification or add additional context in comments.

7 Comments

I need to do this in javascript. I can't use jquery.
What I had that actually worked was the following, but it did not actually replace the data, it only appended it. newData = ''; for (var i=0; i < benefits.length; i++){ newData += '<h4>'+benefits[l].title+'</h4>'; newData += '<p>'+benefits[l].description+'</p>'; } var element = document.querySelector('#memberBenefits li'); element.innerHTML = newData; }
check my updated answer for doing it with javascript
I think it's totally fine to ask the user to accept and upvote the answer if the answer actually works for him :)
check the updated answer for ajax. you should always mention what you have and what you have tried so far in the question description to get the proper answer.
|

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.