I wrote a small basket script, output the purchased goods on the basket page, output the goods as follows: I send ajax request to the product page and try to pull id = "productName" from there. the basket script looks like this:

for (var i = 0; i < localStorage.length; i++) { var limkForProductPage = '/e-store/' + localStorage.key(i);//ссылка на страницу товара $( "#rezult" ).load( "limkForProductPage #productName" ); } 

But the problem is that jquery doesn’t understand that limkForProductPage is a variable and tries to go to mysite.com/limkForProductPage I tried on native js but I don’t understand how I can take an element like

 var xhr = new XMLHttpRequest(); xhr.open('GET', p, false); xhr.send(); if (xhr.status != 200) { alert( xhr.status + ': ' + xhr.statusText ); } else { // вывести результат var xmlDoc = xhr.responseText; } 

how do i get id = "productName"? tried so

  var xmlDoc = xhr.responseText.getElementById('productName'); 

but gives an error

  • one
    Obviously, the error is that the string does not have the getElementById method - Grundy
  • Take out the variable limkForProductPage from quotes. - Jean-Claude

2 answers 2

You can use template lines in ES2015.

 $("#rezult").load(`${limkForProductPage} #productName`); 

Or just build a string

 $("#rezult").load(limkForProductPage + " #productName"); 

In the case of XMLHttpRequest , you can try using the responseXML property, which already has getElementById

Either translate the responseText contents into HTML, and then search.


As a solution to the problem of loading several elements into one, you can load data for different url, into different elements, which are then inserted into the #result main element, for example

 for (var i = 0; i < localStorage.length; i++) { var limkForProductPage = '/e-store/' + localStorage.key(i);//ссылка на страницу товара $( "#rezult" ).append( $('<div></div>').load( limkForProductPage + " #productName" ) ); } 
  • See which problem surfaced when using $ ("# rezult"). load (limkForProductPage + "#productName"); - the last value of the cycle is written to #rezult If there are three items in the basket and the for loop runs three times into #rezult, the last value falls, can this be corrected? - Goshka Tarasov
  • @ GoshkaTarasov, and this is quite logical, you are in the same container phae different data. which will come last. they seem - Grundy
  • Thank! $( "#rezult" ).append( $('<div></div>').load( limkForProductPage + " #productName" ) ); edit $( "#rezult" ).append( $('<div></div>').load( limkForProductPage + " #productName" ) ); - Goshka Tarasov
  • @ GoshkaTarasov, corrected, which is why copying is bad :) - Grundy

Try this:

 for (var i = 0; i < localStorage.length; i++) { var limkForProductPage = '/e-store/' + localStorage.key(i); //ссылка на страницу товара $.get(limkForProductPage).done(function(responce){ var document=$(responce); var element = document.find("#productName"); $( "#rezult" ).append(element); }); } 
  • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky