Adding options to a select using jQuery? - html

i have a button and when i click this button i want a select option added. I can add select but not options, why? I want it added according to the list.
Not adding 'foreach loop option'
$(function () {
var items = ["fa fa-address-book", "fa fa-address-book-o", "fa fa-address-card", "fa fa-address-card-o"];
$('#add-menu').on('click', function (e) {
$('#menu').append('<li>\n' +
'<div class="row">\n' +
'<label class="col-sm-2 col-form-label">Social Media İcon</label>\n' +
'<div class="col-sm-8">\n' +
' <div class="form-group bmd-form-group" id="icon">\n' +
'<select class="form-control selectpicker" id="mySelect" name="icon[]" title="Social Media icon" data-live-search="true" ' +
'data-hide-disabled="true">\n' +
$.each(items, function (i, item) {
$('#mySelect').append($('<option>', {
value: item.value,
text: item.text
}));
}),
'</select>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n' +
'</div>' +
'</li>');
e.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="menus">
<ul id="menu" class="menu">
<li></li>
</ul>
</div>
add-Menu

You're quite close; just replace $.each() with:
items.map(item => `<option>${item}</option>`).join('') +
as in this demo:
$(function () {
var items = ["fa fa-address-book", "fa fa-address-book-o", "fa fa-address-card", "fa fa-address-card-o"];
$('#add-menu').on('click', function (e) {
$('#menu').append('<li>\n' +
'<div class="row">\n' +
'<label class="col-sm-2 col-form-label">Social Media İcon</label>\n' +
'<div class="col-sm-8">\n' +
' <div class="form-group bmd-form-group" id="icon">\n' +
'<select class="form-control selectpicker" id="mySelect" name="icon[]" title="Social Media icon" data-live-search="true" ' +
'data-hide-disabled="true">\n' +
items.map(item => `<option>${item}</option>`).join('') +
'</select>\n' +
'</div>\n' +
'</div>\n' +
'</div>\n' +
'</div>' +
'</li>');
e.preventDefault();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="menus">
<ul id="menu" class="menu">
<li></li>
</ul>
</div>
add-Menu
NOTE
If you intend to add several select tags using the button id="mySelect" will be quite problematic in that you'll end up with more than one element having the same id attribute value. You don't want to end up there.

Related

A cookie issue started with the Instagram api. Something changed with the configuration and it has me baffled

The instafeed api stopped working on my site. The error I am getting is:
A cookie associated with a cross-site resource at https://instagram.com/ was set without the SameSite attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with SameSite=None and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
Chrome changed something and I am trying to understand. Any help would be appreciated.
The site where I wrote this is here. http://galnova.com
var accessToken = '271323200.1677ed0.0a4c06efd9474022b9c3eb47bccf5657'; // use your own token 713486902779818|vM5scnvKTkbBmLbsSgBTPVi3Gqs
$.getJSON('https://api.instagram.com/v1/users/self/media/recent/?access_token='+accessToken+'&callback=?',function (insta) {
$.each(insta.data,function (photos,src) {
if ( photos === 6 ) { return false; }
var date = new Date(parseInt(this.created_time) * 1000);
// template
$(
'<div class="col-sm-6 col-md-3 col-lg-2 grip_wrap flx gonzo">' +
'<div class="grip_hang_topp"></div>' +
'<div class="col grip flex_el flx_ex">' +
'<a title="' + this.caption.text + '" class="fancybox zero lass" data-fancybox="gallery1" data-caption="' + this.caption.text + '" href="' + this.images.standard_resolution.url + '">' +
'<img src="' + this.images.standard_resolution.url + '" />' +
'</a>' +
'<div class="col coat2 truncate">' + this.caption.text + '</div>' +
'<div class="row nill">' +
'<span class="col-6 heart-wrap floated">' + '<i class="fa fa-heart">'+ '</i>' + this.likes.count +' <div class="summ">likes</div>'+'</span>' +
'<span class="col-6 comment-wrap floated">' + '<i class="second fa fa-comment">'+ '</i>' + this.comments.count +' <div class="summ">comments</div>'+'</span>' +
'</div>' +
'<div class="row nill">' +
'<span class="col-sm-12 check-wrap floated">' + '<i class="second fa fa-check">'+ '</i>' + 'Posted ' + (date.getMonth()+1) + '/' + date.getDate() + '/' + date.getFullYear().toString().substring(2) +'</span>' +
'</div>' +
'<div><button class="col-12 btn btn-outline-tertiary">UltraGallery <span class="fab fa-instagram"><\/span></button></div>' +
'</div>' +
'<div class="grip_hang_bott"></div>' +
'</div>'
).appendTo('#instafeed');
});
$(".grip").hover(function(e){
"use strict";
e.preventDefault();
if ($(window).width() > 991) {
return false;
}
});
});
Instagram Api has changed, read more about it here

$.each only the last element is shown

I'm trying to filter a list with AJAX, I have an issue where the html only shows the last element. I have read a lot of similar SO questions and to no avail. I have variables, so I don't know whats the issue.
$("#programme").change(function () {
event.preventDefault();
var selected_programme = $(this).val();
$.ajax({
url: '{% url "gps_list" %}',
data: {
"selected_programme": selected_programme,
},
dataType: 'json',
success: function(response) {
var json = $.parseJSON(response);
$.each( json, function( key, values ) {
var valname = values.fields.name;
var valco = values.fields.country;
var valpro = values.fields.programme_type;
var valwhat = values.fields.what;
console.log(valname, key);
console.log(valco);
console.log(valpro);
console.log(valwhat);
$("#names").html(valname);
$("#country").html(valco);
$("#pro_types").html(valpro);
$("#whats").html(valwhat);
});
},
error: function(response) {
alert("oh no!");
}
});
});
HTML
<div class="row">
<div class="col-md-12">
<h2 class="my-4" id="names">
<small id="country"></small>
</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="card mb-4">
<div class="card-body">
<h2 class="card-title" id="pro_types"></h2>
<p class="card-text" id="whats"></p>
</div>
</div>
</div>
</div>
When you do this:
$("#names").html(valname);
$("#country").html(valco);
$("#pro_types").html(valpro);
$("#whats").html(valwhat);
you're overwriting whatever those elements had in them before. Since you're doing that in a loop, naturally you only see the result of the last write.
You'll need to either:
Write to different elements, or
Append rather than replacing (via append)
The specifics will depend on your HTML structure.
(Now that you've added your HTML.) I would structure it differently. Don't have any row at all (or have a placeholder "loading" row) to start with. Then build rows and append them when you have a response. Something along these lines:
var entryTemplate =
'<div>' +
'<div class="row">' +
'<div class="col-md-12">' +
'<h2 class="my-4">' +
'<span class="names"></span>' + // *** Made it a span inside the h2
'<small class="country"></small>' +
'</h2>' +
'</div>' +
'</div>' +
'<div class="row">' +
'<div class="col-md-12">' +
'<div class="card mb-4">' +
'<div class="card-body">' +
'<h2 class="card-title pro_types"></h2>' +
'<p class="card-text whats"></p>' +
'</div>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
function success(response) {
var json = $.parseJSON(response);
var container = $("#container");
container.empty(); // Removes any previous rows
$.each( json, function( key, values ) {
var fields = values.fields;
var entry = $(entryTemplate);
entry.find(".names").text(fields.name);
entry.find(".country").text(fields.country);
entry.find(".pro_types").text(fields.programme_type);
entry.find(".whats").text(fields.what);
container.append(entry.children());
});
}
success(
'[' +
'{"fields": {' +
'"name": "Name 1",' +
'"country": "Country 1",' +
'"programme_type": "PT 1",' +
'"what": "What 1"' +
'}},' +
'{"fields": {' +
'"name": "Name 2",' +
'"country": "Country 2",' +
'"programme_type": "PT 2",' +
'"what": "What 2"' +
'}},' +
'{"fields": {' +
'"name": "Name 3",' +
'"country": "Country 3",' +
'"programme_type": "PT 3",' +
'"what": "What 3"' +
'}}' +
']'
);
setTimeout(function() {
success(
'[' +
'{"fields": {' +
'"name": "Name 4",' +
'"country": "Country 4",' +
'"programme_type": "PT 4",' +
'"what": "What 4"' +
'}},' +
'{"fields": {' +
'"name": "Name 5",' +
'"country": "Country 5",' +
'"programme_type": "PT 5",' +
'"what": "What 5"' +
'}},' +
'{"fields": {' +
'"name": "Name 6",' +
'"country": "Country 6",' +
'"programme_type": "PT 6",' +
'"what": "What 6"' +
'}}' +
']'
);
}, 2000);
<div id="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Bootbox message box doesn't fit content

bootbox.confirm({
title: "請輸入綁定手機號碼",
message:
'<div class="col-xs-12">'
+ '<form class="form-horizontal">'
+ '<div class="form-group">'
+ '<label class="col-xs-12" style="text-align: left">帳號(您的手機號碼):</label>'
+ '<div class="col-xs-5">'
+ '<select class="form-control" id="countryCode-bootbox" name="countryCode-bootbox">'
+ '<option value="+93">Afghanistan +93</option>'
+ '<option value="+355">Albania +355</option>'
+ '<option value="+213">Algeria +213</option>'
+ '<option value="+1684">American Samoa +1684</option>'
+ '<option value="+263">Zimbabwe +263</option>'
+ '</select>'
+ '</div>'
+ '<div class="col-xs-7">'
+ '<input type="text" class="form-control" id="cellphone-bootbox" name="cellphone-bootbox">'
+ '</div>'
+ '</div>'
+ '</form>'
+ '</div>',
callback:
function(result) {
if(result == false) {
return;
}
register3rdPartyUser(flag, $('#countryCode-bootbox').val(), $('#cellphone-bootbox').val());
}
});
As you can see, there are two horizontal lines inside the overall box which I believe are the boundaries of the message content. The bottom line doesn't come after the message content when I'm using HTML for my string. How can I fix this issue? Is it a bug?

datepicker not working for dynamically created datepicker

html:
<p id="drag11" class="ui-widget-content" > Calender </p>
<div class="col-md-8"> <div id="droppable" class="ui-sortable" ><ol></ol></div></div>
jquery ui:
var calenderFieldCount++=0;
calenderFieldCount++;
$("#droppable").append('<li class="ui-state-default">' +
'<div class="cal" id="InputsWrapper_11' + calenderFieldCount + '">' +
'<label class="cal"> calender:' + calenderFieldCount + '</label>' +
'<p>' + '<input type="text" id="datepicker" class="hasDatepicker" name="mycal[]" />'+
'<button class="removeclass11">x</button>' +
'</p>' + '<br>' + '</div>' + '</li>');
$("#droppable").delegate(".hasDatePicker", "click", function () {
$(this).datepicker();
});
when element with id= "drag11" is dragged and dropped in div with id="droppable" a textbox appears but when you click on the textbox the datepicker dose not appear. the above code is used. please help. thank you in advance.
try this jquery date picker just add field id and use js file
<script>
jQuery(document).ready(function()
$('#dob').datepicker({
dateFormat: 'dd-mm-yy'
});
});
</script>

Autodividers in listview in JQM using filter

I am using autodividers in listview in JQM and populating new "pages" which works fine when all the list is displayed but when user filters the list, the corresponding page does not display. I'm sure I'm missing something obvious so would appreciate any help.
js
$(document).ready (function() {
$.getJSON("eur_countries_array.json", function (data) {
var countries = [];
for (var obj in data){
countries.push({
title: data[obj].title,
flag: data[obj].flag,
population: data[obj].population,
avg_annual_growth: data[obj].avg_annual_growth,
date: data[obj].date
});
}
// call sort function
countries.sort(compare);
// read through array
var listHTML = "";
for (var i=0; i < countries.length; i++) {
// for each country add flag and title to list
listHTML += '<li><img class="ui-li-icon ui-corner-none alt="'+ countries[i].flag + 's flag" src="flags/16/' + countries[i].flag +'.png" >' + countries[i].title + '</li>';
// Append new "pages" after "home" page for each country
var pHTML = '<div data-role="page" id="' + countries[i].flag + '">';
pHTML += '<div data-role="header"><h1>' + countries[i].title + '</h1><a data-rel="back">Back</a></div>';
pHTML += '<div role="main" class="ui-content" class="country_details">';
pHTML += '<div class="center"><img class="flag_display" alt="'+ countries[i].flag + 's flag" src="flags/64/' + countries[i].flag +'.png" ></div></div>';
pHTML += '<div><p class="center">Population of ' + countries[i].title + ' is ' + countries[i].population + '</p></div>';
pHTML += '<div><p class="center">Average annual growth: ' + countries[i].avg_annual_growth + '</p></div>';
pHTML += '<div><p class="center">Statistics correct as of: ' + countries[i].date + '</p></div>';
pHTML += '</div>';
console.log(pHTML);
$("body").append(pHTML);
}
// Display countries list
// call the refresh method to update the visual styling after adding new elements
$("#countryList").empty().append(listHTML).listview("refresh");
});
});
html
<div data-role="page" id="home">
<div data-role="header">
<h2>European Countries</h2>
</div>
<div role="main">
<ul id="countryList" data-role="listview" data-autodividers="true" data-filter="true" data-inset="true">
<li></li>
</ul>
</div>
<div data-role="footer">
</div>
</div><!-- page -->