1

Looked through the topics but found no full answer.

My task is to fill a drop-down with text from another HTML file. Both HTML files are on localhost. Having data in JSON is a requirement.

Initially, came into following:

$.getJSON("feed.html", function (data) {
        $.each(data, function (index, item) {
            $('#insider').append($('<option</option>').val(item).html(item));
        });
    });

Although, this does not seems to work as getJSON does not seem to operate with html but .json files instead.

Problem is second file has to remain in .html format. Are there any other possibilities how to have whole html text inserted as json without having separate .json file?

1
  • It doesn't make sense to get HTML into JSON and into HTML again without having any server side script doing this. What you want to do is make an AJAX request for a HTML file, extract the data you need and then insert that data into the current HTML file. Commented Mar 12, 2017 at 20:30

2 Answers 2

1

You can use jQuery.ajax with the json dataType parameter:

$.ajax({
    url: "feed.html",
    dataType: "json",
}).done(function(data) {
    console.log(data);
});

And in feed.html:

[{"hello":"world"}]

This will return a JSON object as the data value in the .done callback. So with your code:

$.ajax({
    url: "feed.html",
    dataType: "json",
}).done(function(data) {
    $.each(data, function (index, item) {
        $('#insider').append($('<option</option>').val(item).html(item));
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, above appended none. Html I am querying to contains of usual text in p and h# tags.
0

You can use get http://api.jquery.com/jQuery.get/

$.get( "feed.html", function( data ) {
   var parsedHtml = [];

   // parse/split the html up here and push it onto the array
   ....

   $.each(parsedHtml , function (index, item) {
      $('#insider').append($('<option</option>').val(item).html(item));
   });
});

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.