Assigning a text within JAVASCRIPT methods?

Asked

Viewed 42 times

0

I have a problem need to insert a text before returning a request in a api:

getSW();

function getSW() {

  $.getJSON("https://swapi.co/api/films", function (json) {
    var data = JSON.parse(JSON.stringify(json));

    var films = {
        f1: data.results[2],
        f2: data.results[1],
        f3: data.results[3],
        f4: data.results[0],
        f5: data.results[5],
        f6: data.results[4],
        f7: data.results[6],
    };


    for (var i = 1; i < 8; i++) {if (window.CP.shouldStopExecution(0)) break;
      $("main .row").append(
      "<div class='col-12 col-sm-6 col-md-4'>" +
      "<div class='item films'>"
         +  "<h2 class='title'>" + films["f" + i].title.toLowerCase() + "</h2>" + "<br />" +
         +  "<h1 class='title'>" + films["f" + i].director + "</h1>" +
      "<div class='crawls hidden'><div>" +
            "<h2>" + films["f" + i].title.toLowerCase() + "</h2>" +
            "<p class=''>" + films["f" + i].opening_crawl + "</p>" +
      "</div></div>" +
      "</div>" +
      "</div>");

I need to insert a text before films["f" + i] ....

  • would be >" + "Texto Aleatório" + films["f" + i] ...

  • I’ve tried it even carries the main ...

  • it is complicated to know its purpose, but, at the beginning of your address to correctly redeem only the json: https://swapi.co/api/films/?format=json ... what needs to be done?

  • Paste a text before the api answer for example + "<H1 class='title'>" NAME OF THE DIRECTOR + films["f" + i]. director + "</H1>" +

  • I passed this in the first comment! now your code is not giving any error?

  • Sorry I hadn’t seen it, it worked but it appears as nanwarden...

  • I made a functional example right below!

  • I tried to apply the idea without success

  • You can’t do this here but, it’s because if you put single quotes on everything inside put double quotes will work yes! here for example it should be like this: '<div class="col-12 col-sm-6 col-md-4">' or otherwise: "<div class='col-12 col-sm-6 col-md-4'>"

Show 4 more comments

1 answer

1


With an example simple, you can add a Different Text, in its code as demonstrated just below:

getSW();

function getSW() {
  $.getJSON("https://swapi.co/api/films/?format=json", function(json) {
    $("div.row").empty();
    $.each(json.results, function(i, it) {
      let text = '';
      text = text + '<div>';
      text = text + '<h4>' + 'Texto Diferente - ' + it.title + '</h4>';
      text = text + '</div>';
      text = text + '<hr/>';
      $("div.row").append(text);
    });
  })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row"> Carregando ... </div>

in its original code is very verbose, a lot of writing and wrong concepts, because one needs to know the results dynamically indicate only the result of json.results.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.