The loop is not functioning according to its design

http://codepen.io/abdulahhamzic/pen/aNexYj

I have been attempting to implement a loop to display ten results on the screen, but it appears that there is an issue with my loop that I am unable to identify. In Mozilla, only one result is being shown on the screen and it seems that the loop stops after the first iteration. On Chrome, I do see all ten results but it seems like the loop continues to run as I cannot style the newly created elements, and I also notice a loading icon on the page. Can anyone guide me in fixing this loop? Below is the code snippet:

var url = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=aa&limit=10&callback=?";
$(document).ready(function(){
  $.getJSON(url, function(json){
    for (var i = 0; i < json[1].length; i++){
      document.write("<div><span class='header' style='color:red'>" + json[1][i] + "</span><br>" + json[2][i] + "</div><br>")
    }
  })
})    

Answer №1

Avoid using the document.write() function as it can lead to unexpected behavior across different browsers. Instead, consider implementing the following code snippet:

var url = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=aa&limit=10&callback=?";
$(document).ready(function(){
  $.getJSON(url, function(json){
    for (var i = 0; i < json[1].length; i++){
      $("body").append("<div><span class='header' style='color:red'>" + json[1][i] + "</span><br>" + json[2][i] + "</div><br>")
    }
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The search bar - just the initial choice is functional

I am having some trouble with my search box. It has two options, 'search catalog' and 'search site', each with different urls. However, only the 'search catalog' option seems to be working properly. When I select 'search ...

CSS animation shifting while altering its color palette

I have limited experience with CSS animations and my client is looking to achieve a hover effect similar to the one shown here when hovering over the contact button: Just to clarify, they are looking for: The square to move from left to right and then b ...

Rendering data from an API using v-if

Could you help me change the tag that currently displays true or false? I want it to show "FREE" if the event is free and "PAID" if it's not. Check out the Eventbrite API here The response I'm receiving from data.events.is_free is in boolean fo ...

Is it possible for HTML/CSS styling for print, background color, and image not to display in both IE and Firefox?

Is there a way to display the background color and image when printing? I understand this is typically controlled by browser properties, but I'm looking to customize it with CSS. For example, on webkit, I use -webkit-print-color-adjust: exact;. How ca ...

Custom cellRenderer prevents Ag Grid's autoHeight and wrapText features from functioning properly

I've been attempting to adjust the formatting of a long cell value by wrapping the text. According to the documentation, setting autoHeight=true and wrapText=true works fine without any cellRenderer components. However, when using a cellRendererFramew ...

In this context, 'number' is typically a data type, but it seems to be functioning as a specific value

Greetings! I'm encountering an issue with this Angular 4 code. Whenever I attempt to type a number, I encounter the following error: 'number' only refers to a type, but is being used as a value here.ts(2693). View image description here ...

Require a date picker in Vuetify, prohibiting any kind of text input

I am working with vuetify to create a date picker for capturing a user's birthday. I need the date picker to be a required field in my form and only accept date values. Here is what I have attempted: <v-flex xs12> <v-menu ref="menu" ...

Issues with @material-ui/core and react-bootstrap imports are causing disruptions to the NextJS build process

After researching, I've come to realize that this issue is common among developers. Unfortunately, none of the solutions I found have worked for me. During npm run dev mode, everything in the project functions as expected, with all imports working se ...

Utilize the JavaScript Email Error Box on different components

On my website, I have implemented a login system using LocalStorage and would like to incorporate an error message feature for incorrect entries. Since I already have assistance for handling email errors on another page, I am interested in applying that sa ...

Using AJAX to retrieve additional JavaScript code or functions from the server

It's common knowledge that AJAX is often utilized to request a web part in HTML format from the server. However, is it feasible to use AJAX to request a script that includes functions? ...

Is it possible to assign a name attribute to the <img> tag in HTML?

Can the "name" attribute be used in an "img" tag in HTML? If you have this knowledge, please do share and thank you in advance. ...

Vue.js Conditional Templates

I am attempting to implement VueJs conditional rendering using handlebars in vueJs 2.0 as outlined in their official documentation, but eslint is throwing an error: - avoid using JavaScript keyword as property name: "if" in expression {{#if ok}} - avoid us ...

When I navigate using state.go in my controller, the Ionic slide menu fails to appear

Hello everyone, I am currently using Ionic to develop my application and have implemented a slide menu. However, I encountered an issue where the slide menu fails to work when changing views using "$state.go". How can I resolve this? The Router : "use st ...

Switching the phone formatting from JavaScript to TypeScript

Below is the JavaScript code that I am attempting to convert to TypeScript: /** * @param {string} value The value to be formatted into a phone number * @returns {string} */ export const formatPhoneString = (value) => { const areaCode = value.substr(0 ...

Creating a CSS3 Grid Layout with a Fixed Header

I'm having some trouble creating a layout using CSS Grid. My goal is to make the header element 'sticky', so that it remains fixed at the top of the viewport while other content scrolls up and underneath it. Additionally, I want to add a bac ...

Varied animation response for the secondary background image

Attempting to design a sidebar menu with CSS animations and striving to achieve the maximum without relying on JavaScript or JQuery. However, if necessary, using JS / JQuery is acceptable. The challenge at hand involves an <span></span> elemen ...

What is the process for extracting the period value from SMA technical indicators within highcharts?

Can someone assist me in retrieving the period value from SMA indicator series by clicking on the series? series : [{ name: 'AAPL Stock Price', type : 'line', id: 'primary', ...

Exploring Firebase database with AngularJS to iterate through an array of objects

I'm working on an app that pulls data from a Firebase database, but I'm running into an issue. When I try to loop through the data and display it on the page, nothing shows up. However, if I use console.log to output the data, it's all there ...

An error occurred while attempting to reset your password on Parse.com (JS SDK) due to an

I am having trouble resetting my password in my angularjs App. I am utilizing Parse (js SDK) as the backend. Even though I am following the documentation and using Parse.User.requestPasswordReset, I keep encountering error 125 which states invalid email ad ...

Conceal and reveal buttons at the same location on an HTML webpage

There are 3 buttons on my custom page called page.php: <input type="submit" value="Btn 1" id="btn1"> <input type="submit" value="Btn 2" id="btn2" onClick="action();> <input type="submit" value="Btn 3" id="btn3" onClick="action();> ...