jQuery fadeIn then fadeOut text from innerHTML not working - html

I have a jQuery function to fade text in and out one by one. However the function is not working correctly due to I place the text from jQuery. When I try to run it, it is only showing 0. 1 to 4 is not shown.
var html = '<div id="main">';
for (var i = 0; i < 5; i++) {
html += '<div class="trip">' + i + '<div>';
}
html += '</div>';
document.getElementById('todayPrayTime').innerHTML = html;
var $elem = $('#main .trip'),
l = $elem.length,
i = 0;
function go() {
$elem.eq(i % l).fadeIn(700, function() {
$elem.eq(i % l).fadeOut(700, go);
i++;
})
}
go()
.trip {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="todayPrayTime"></div>
Fiddle.

var html = '<div id="main">';
for (var i = 0; i < 5; i++) {
html += '<div class="trip">' + i + '</div>';
}
html += '</div>';
document.getElementById('todayPrayTime').innerHTML = html;
function go($list, i) {
var l = $list.length, $trip = $list.eq(i % l);
$trip.show().fadeIn(700, function(){
$trip.fadeOut(700, function(){
$trip.hide();
if ( ++i < l ) go($list, i);
});
})
}
go($('#main .trip'), 0)
.trip {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="todayPrayTime"></div>
var html = '<div id="main">';
for (var i = 0; i < 5; i++) {
html += '<div class="trip">' + i + '</div>';
}
html += '</div>';
document.getElementById('todayPrayTime').innerHTML = html;
function go($list, i) {
var l = $list.length, $trip = $list.eq(i);
$trip.show().fadeIn(700, function(){
$trip.fadeOut(700, function(){
$trip.hide();
go($list, (++i) % l);
});
})
}
go($('#main .trip'), 0)
.trip {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="todayPrayTime"></div>

Related

Jquery using append/appendTo with time delay

function a(response) {
setTimeout(function () {
for (i = 0; i < response.length; i++) {
if (response[i].hasOwnProperty("text")) {
var b = '<img class="img" src="path of my pic"/><p class="someclass">' + response[i].text + '</p><div class="something"></div>';
$(b).appendTo(".chatwindow").hide().fadeIn(1000);
} } } }
How do I use appendTo/append the content one after the other with a time delay? Instead it is displaying all at once, which I dont want like that.Please help me if i have to change anything?
You can use the delay() method for this:
function appendWithDelay() {
for (i = 0; i < 10; i++) {
var template = '<img class="img" src="https://picsum.photos/100"/>';
$(template).hide().appendTo(".chatwindow").delay(i * 1000).fadeIn(1000);
}
}
appendWithDelay();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="chatwindow"></div>
Place your settimeout function inside the loop and condition
function a(response) {
for (i = 0; i < response.length; i++) {
if (response[i].hasOwnProperty("text")) {
setTimeout(function () {
var b = '<img class="img" src="path of my pic"/><p class="someclass">' + response[i].text + '</p><div class="something"></div>';
$(b).appendTo(".chatwindow").hide().fadeIn(1000);
}, 1000)
}
}
}

How to push elements in array and print the elements of array in jquery

Here I tried to push my elements into array but I am not getting output in the p tag. May be there is any mistake in the code. Can someone help me out ?
<script type="text/javascript">
jQuery(document).ready(function()
{
var arr = [];
var text = "";
jQuery("#txtResult").click(function()
{
//var text = "";
//var string = jQuery("#txtPrint").html();
for (i = 0; i < arr.length; i++)
{
if (jQuery("#txtFirstNo").val().length == 5)
{
arr.push("QQQ");
}
if (jQuery("#txtFirstNo").val().length == 5)
{
arr.push("AA");
}
text += arr[i] + "<br>";
}
jQuery("p").html(text);
});
});
</script>
</head>
<body>
<input type="text" id="txtFirstNo" name="txtFirstNo" placeholder="Enter value" />
<input type="submit" id="txtResult"/>
<p id="txtPrint"></p>
</body>
</html>
Simplified, looks like you just wanted smth like this
$("#txtResult").click(function(){
let text = "";
if ($("#txtFirstNo").val().length == 5) {
text = "QQQ" + "<br>" + "AA" + "<br>";
}
$("p").html(text);
}

nan error in load Advertising box inside the li element

i write this code, it's right but show nan error after Advertising img.
Does anyone know where this error comes from and how can I fix it?
my code in html:
<div>
<ul class="list-groupJournalList">
</ul>
</div>
my code in jquery:
jQuery(document).ready(function ($) {
for (var i = 1; i <= 100; i++) {
inner1 = ('<li class= "list-group-item">' +
'<div class="col-xs-12 article-wrapper">' + '</div>' +
'</li>');
advboxx = ('<li><div class="advertising-SearchReasultBox">' +
'<img src="../FrontEnd/media/image/Sid-adv1-moavenatelmi.gif" height="80"/>' +
+'</div></li>');
for (k = 1; k < 3; k++) {
$('.list-groupJournalList').append(inner1);
}
$('.list-groupJournalList').append(advboxx);
}
The output is similar to the image below:
nan error in image
I think you forgot to close your function, the "for" loop and there's an extra "+" here at the beginning of the 3rd line :
advboxx = ('<li><div class="advertising-SearchReasultBox">' +
'<img src="../FrontEnd/media/image/Sid-adv1-moavenatelmi.gif" height="80"/>' +
+'</div></li>');
Try this :
jQuery(document).ready(function ($) {
for (var i = 1; i <= 100; i++) {
inner1 = '<li class= "list-group-item"> <div class="col-xs-12 article-wrapper"> </div> </li>';
advboxx = '<li><div class="advertising-SearchReasultBox"> <img src="../FrontEnd/media/image/Sid-adv1-moavenatelmi.gif" height="80"/> </div> </li>';
for (k = 1; k < 3; k++) {
$('.list-groupJournalList').append(inner1);
}
$('.list-groupJournalList').append(advboxx);
}
});

Chrome Storage API keys getting reset

I am developing my first chrome extension. It highlights the words that are passed to it. I used storage API to save a few objects so when I open the extension again, it will retain what I highlighted. but once in a while, they get reset to the first string value of the object only.
https://www.screencast.com/t/0jF6AtCc
I went through the code for hours but there is nothing that could be doing this. i have tried them reassigning them in multiple places but nothing helped. There are no errors in both consoles.
popup.js
var rstr = [];
var rnum = [];
var rcolor = [];
var rfont = [];
chrome.storage.local.get(['htxt', 'hnum', 'hcolor', 'hfont'], function(result) {
if (result.hnum != null) {
rnum = (result.hnum);
rcolor = (result.hcolor);
rfont = (result.hfont);
rstr = (result.htxt);
}
if (rstr.length > 0) {
document.getElementById('tbl').innerHTML = '<tr><td>Search Key</td><td style="width: 25px;">Score</td><td></td><td></td></tr>';
for (var j = 1; j <= rstr.length; j++) {
document.getElementById('tbl').innerHTML += '<tr><td><input type="text" id="txt' + j + '"></input></td><td><input type="text" id="num' + j + '" style="width: 25px;" value=0></input></td><td><input type="color" id="bg' + j + '" style="width: 15px;"> </td> <td> <input type="color" id="font' + j + '" style="width: 15px;"> </td> </tr> ';
}
for (var j = 1; j <= rstr.length; j++) {
document.getElementById("txt" + j).value = rstr[j - 1];
document.getElementById("num" + j).value = rnum[j - 1];
document.getElementById("bg" + j).value = rcolor[j - 1];
document.getElementById("font" + j).value = rfont[j - 1];
}
}
colorarr = ["#b1c7fd", "#ddfdb1", "#fdb1f3", "#b1fdf0", "#fddab1", "#c4b1fd", "#b4fdb1", "#FFFF00", "#FFA500"];
function getRandomColor() {
return colorarr[Math.floor(Math.random() * 9)];
}
document.getElementById("bg1").value = getRandomColor();
var i = 2;
var txt = [];
var num = [];
var color = [];
var font = [];
document.getElementById('btn').addEventListener('click', function() {
chrome.tabs.query({
active: true,
currentWindow: true
}, function(activeTabs) {
txt = [];
num = [];
color = [];
font = [];
for (var j = 1; j < i; j++) {
if (document.getElementById("txt" + j).value != null) {
txt.push(document.getElementById("txt" + j).value);
num.push(document.getElementById("num" + j).value);
color.push(document.getElementById("bg" + j).value);
font.push(document.getElementById("font" + j).value);
}
}
chrome.storage.local.set({
"htxt": txt
});
chrome.storage.local.set({
"hnum": num
});
chrome.storage.local.set({
"hcolor": color
});
chrome.storage.local.set({
"hfont": font
});
chrome.tabs.executeScript(null, {
code: "var str=" + JSON.stringify(txt) + ";var numjson=" + JSON.stringify(num) + ";var color=" + JSON.stringify(color) + ";var font=" + JSON.stringify(font) + ";"
}, function() {
chrome.tabs.executeScript(null, {
file: 'highlight.js'
}, function(results) {
if (results != null) {
document.getElementById('scorevalue').innerHTML = results[0];
chrome.browserAction.setBadgeText({
text: results[0] ? results[0].toString() : "0"
});
}
});
});
});
});
document.getElementById('add').addEventListener('click', function() {
var tr = document.createElement('tr');
tr.innerHTML = '<td><input type="text" id="txt' + i + '"></input></td><td><input type="text" id="num' + i + '" style="width: 25px;" value=0></input></td><td><input type="color" id="bg' + i + '" style="width: 15px;"> </td> <td> <input type="color" id="font' + i + '" style="width: 15px;"> </td> ';
document.getElementById('tbl').appendChild(tr);
document.getElementById("bg" + i).value = getRandomColor();
i++;
});
document.getElementById('delete').addEventListener('click', function() {
chrome.storage.local.remove(['htxt', 'hnum', 'hcolor', 'hfont'], function() {
var error = chrome.runtime.lastError;
if (error) {
console.error(error);
}
})
document.getElementById('tbl').innerHTML = '<tr><td>Search Key</td><td style="width: 25px;">Score</td><td></td><td></td></tr>';
document.getElementById('tbl').innerHTML += '<tr><td><input type="text" id="txt1"></input></td><td><input type="text" id="num1" style="width: 25px;" value=0></input></td><td><input type="color" id="bg1" style="width: 15px;"> </td> <td> <input type="color" id="font1" style="width: 15px;"> </td> </tr> ';
chrome.tabs.getSelected(null, function(tab) {
var code = 'window.location.reload();';
chrome.tabs.executeScript(tab.id, {
code: code
});
});
});
document.getElementById('bulk').addEventListener('click', function() {
document.getElementById('bulkdiv').innerHTML = "<textarea rows='4' cols='50' id='bulktext'></textarea><input id='bulkbtn' type=button value='Fill Up'>"
document.getElementById('bulkbtn').addEventListener('click', function() {
var res = document.getElementById('bulktext').value.split("\n");
i = res.length + 1;
document.getElementById('tbl').innerHTML = '<tr><td>Search Key</td><td style="width: 25px;">Score</td><td></td><td></td></tr>';
for (var j = 1; j < i; j++) {
document.getElementById('tbl').innerHTML += '<tr><td><input type="text" id="txt' + j + '"></input></td><td><input type="text" id="num' + j + '" style="width: 25px;" value=0></input></td><td><input type="color" id="bg' + j + '" style="width: 15px;"> </td> <td> <input type="color" id="font' + j + '" style="width: 15px;"> </td> </tr> ';
}
for (var j = 1; j < i; j++) {
document.getElementById("txt" + j).value = res[j - 1].trim();
document.getElementById("bg" + j).value = getRandomColor();
}
console.log(res);
});
});
});
Highlight.js
function highlight(container, what, spanClass) {
var content = container.innerHTML,
pattern = new RegExp('(>[^<.]*)(' + what + ')([^<.]*)', 'gi'),
replaceWith = '$1<span ' + (spanClass ? 'style=background:' + spanClass + ';' : '') + '">$2</span>$3',
highlighted = content.replace(pattern, replaceWith);
return (container.innerHTML = highlighted) !== content;
}
function num(container, what) {
if ((container.innerHTML.match(new RegExp('(>[^<.]*)(' + what + ')([^<.]*)', "gi"))) != null) return container.innerHTML.match(new RegExp('(>[^<.]*)(' + what + ')([^<.]*)', "gi")).length;
return 0;
}
chrome.storage.local.remove(['htxt', 'hnum', 'hcolor', 'hfont'], function() {
var error = chrome.runtime.lastError;
if (error) {
console.error(error);
}
var score = 0;
for (var i = 0; i < str.length; i++) {
if (str[i] != '') {
console.log("inside highlight.js");
console.log(str[i]);
highlight(document.getElementsByTagName('body')[0], str[i], color[i]);
score += num(document.getElementsByTagName('body')[0], str[i]) * numjson[i];
}
}
chrome.storage.local.set({
"htxt": str
});
chrome.storage.local.set({
"hnum": numjson
});
chrome.storage.local.set({
"hcolor": color
});
chrome.storage.local.set({
"hfont": font
});
score;
});
There is an inject.js also which run on all pages with similar code to highlight.js. It was made so that pages get highlighted even when someone navigate away or reload the page. It is also accessing the Chrome Storage API.
Only popup.js have code to modify the key pairs in storage API but it still somehow get changed to the first string. Please help.
Here is an unpacked version of it.
https://drive.google.com/file/d/1icG6pa9xTGAJ6DO2Wgwn7LE5yG8CSRzz/

Div not maintaining min-width inside another div, HTML

I have three classes of div containers. One is an outer container
with width:1200px, with inner div rows that each have inner div's with a
desired size of 280 px. My problem is that when I create the elements
using the DOM that the CountriesInnerDiv elements are varying in size depending what is inside them.
Here's what I have so far:
.CountriesOuterDiv1{
width: 1200px;
}
.CountriesInnerDiv{
min-width:280px;
display:inline;
border:1px solid black;
}
.RowDiv {
width:100%;
}
Here is the relevant Javascript:
function AddTable() {
var TableDiv = document.getElementById("CountriesOuterDiv");
var i,j;
var NumCountries = 5;
var NumSectors = 5;
for (i = 0; i < NumCountries; i++) {
var RowDiv = document.createElement("div")
RowDiv.className = "RowDiv";
RowDiv.id = "Row"+i;
for (j = 0; j < NumSectors + 1; j++) {
var CountryData = document.createElement("div");
var SectorString = String(i+1)+"_"+String(j);
CountryData.className = "CountriesInnerDiv";
CountryData.id = "CountryData"+SectorString;
if (!((i ==0 && j == 0) || (i==0 && j ==1))) {
CountryData.style.display = "none";
}
if (j == 0 || (i == 0 && j == 1)) {
var CountryDataSelect = document.createElement("select");
var AddButton = document.createElement("button");
AddButton.id = "AddButton" + SectorString;
AddButton.innerHTML = "+";
if (j != 0) {
AddButton.setAttribute('onclick','AddOrDeleteDiv("add", "' + SectorString + '");');
}
else {
if (i != NumCountries - 1) {
AddButton.setAttribute('onclick','AddCountry("' + SectorString + '")');
}
if (i != 0) {
var DeleteCountry = document.createElement("button");
DeleteCountry.id = "DeleteButton" + SectorString;
DeleteCountry.innerHTML = "-";
DeleteCountry.setAttribute('onclick', 'DeleteCountry("' + SectorString + '")');
}
}
CountryData.appendChild(CountryDataSelect);
if (i != NumCountries - 1) {
CountryData.appendChild(AddButton);
}
if (i!=0) {
CountryData.appendChild(DeleteCountry);
}
RowDiv.appendChild(CountryData);
}
else if (j == NumSectors) {
var CountryDataSelect = document.createElement("select");
var DeleteButton = document.createElement("button");
DeleteButton.id = "DeleteButton" + SectorString;
DeleteButton.innerHTML = "-";
DeleteButton.setAttribute('onclick','AddOrDeleteDiv("delete", "' + SectorString + '");');
CountryData.appendChild(CountryDataSelect);
CountryData.appendChild(DeleteButton);
}
else {
var CountryDataSelect = document.createElement("select");
var AddButton = document.createElement("button");
var ADString = "add";
AddButton.setAttribute('onclick','AddOrDeleteDiv("add", "' + SectorString + '");');
AddButton.id = "AddButton" + SectorString;
AddButton.innerHTML = "+";
var DeleteButton = document.createElement("button");
DeleteButton.id = "DeleteButton" + SectorString;
DeleteButton.innerHTML = "-";
DeleteButton.setAttribute('onclick','AddOrDeleteDiv("delete", "' + SectorString + '");');
CountryData.appendChild(CountryDataSelect);
CountryData.appendChild(AddButton);
CountryData.appendChild(DeleteButton);
}
RowDiv.appendChild(CountryData);
TableDiv.appendChild(RowDiv);
}//End of inner for
}//End of outer for
}//end of function
Its cose you make this div display:inline; do like this .CountriesInnerDiv{
min-width:280px;
display:inline-block;
border:1px solid black;
}
Basically, what you're experiencing, is most likely that the inner element is just too big in it's minimal size to fit the parent one when the window is small... well.. we can't have an inside bigger than an outside... unless you use
overflow: scroll;
This will make the parent element scrollable so you can visualize the child by scrolling...
or...
.parent{min-width: 280px}
wich will make the parent has the same minimum width...