I work with the text entered by the user, for a start I sort the line on the space, and then if the word is a link to the picture, I print it out, if not, I leave it in plain text and add it to the final message. Why, when executing a function, the argument in checkImage arr[i] becomes undefined and an empty link is obtained instead of a picture. I can not understand why the function does not see the element of the array?
var text = document.getElementById("text1").value; var arr = text.split(" "); // делим строку по пробелу var mess = new Array(); // новое сообщение for (var i = 0; i < arr.length; i++) { if (arr[i].indexOf('.jpg') + 1 || arr[i].indexOf('.png') + 1 || arr[i].indexOf('.bmp') + 1 || arr[i].indexOf('.gif') + 1 || arr[i].indexOf('.jpeg') + 1) // если есть какое-либо расширение { checkImage(arr[i], // проверка ссылки, рабочая - картинка, нерабочая - // текст function() { var newr = document.createElement("a"); newr.href = arr[i]; newr.target = "_blank"; newr.innerHTML = '<img src= ' + arr[i] + ' style = "max-width:90%">'; f1.appendChild(newr); }, mess.push(" " + arr[i])) } else { mess.push(" " + arr[i]); } } var newr = document.createElement("div"); // создать и вывести текст newr.innerHTML = mess; f1.appendChild(newr);