1

I have my json returning html. Shown below is the ajax bit:

$.ajax({
                type: 'POST',
                url: area.wrapper.attr('data-ajax'),
                data: data_submit,
                dataType: 'json',
                success: function( data )
                {
                      console.log(data.content);
                } 
});

console.log(data.content); returns the html shown below:

<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$63.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>


<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$68.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>


<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$58.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>


<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$50.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>

From the above html response, i want to capture the prices that are under the class:

<span class="gdlr-tail" id = "gdlr-currencyprice">$63.00 / Night</span>

Now, the next step is should be retrieving the value from the class and this should be the way to go:

$(".gdlr-tail").each(function(i) {}

so to my question, how from my ajax response: data.content, should i capture my class attribute(gdlr-tail) to pass it to my iterating function to access my values? hope my question is clear

5
  • Why not just return only the HTML you actually need? Using JSON to wrap HTML is pretty redundant. Also the HTML you generate is invalid as you've repeated the same id; they need to be unique Commented Oct 18, 2016 at 7:05
  • The html is generated from a backend script that iterates hence the same id. Would be interested though on advice on how to return HTML that i ONLY need. thanks Commented Oct 18, 2016 at 7:11
  • You can iterate without using the same ID twice. If you need each element to have the same selector, use a class. Using an ID for this totally defeats the purpose of having an id. Commented Oct 18, 2016 at 7:20
  • As per Rory's suggestion you can just return the HTML as a string from your server side script & then put it in desirable parent or anything else.For this you will need to change the dataType attribute of your ajax to html instead of json Commented Oct 18, 2016 at 7:24
  • I get your point @techie_28 and that would work but apart from the html, there's more being returned from the response so it needs to remain as ajax. Commented Oct 18, 2016 at 7:34

3 Answers 3

3

With jQuery, you could just do: $(data.content).find('.gdlr-tail').each(...)

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

Comments

1

try this

$(data.content)
    .find('.gdlr-tail')
        .each(function(i,el){
            alert(el);
         });

Comments

0

Use RegExp to extract the values:

var html = '<div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$63.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div><div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$68.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div><div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$58.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div><div class="gdlr-booking-room-wrapper" ><img src="" alt="" width="400" height="300" /></a></div><span class="gdlr-head">Start From</span><span class="gdlr-tail" id = "gdlr-currencyprice">$50.00 / Night</span></div><div class="clear"></div></div><div class="clear"></div></div></div>';

var result = html.match(/\$[\d\.]+ \/ [^<]+/gi);

console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1 Comment

Excellent. all the answers are technically right but this one hammers the point

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.