Bootstrap theme not working through script - html

I'm trying to retrieve the country code from our API.
$(document).ready(function() {
$.ajax({
url: "http://api.ourapi",
dataType: "json",
success: function( countries ) {
var selectHTML = "";
selectHTML="<select>";
for(i = 0; i < countries.length; i = i + 1) {
country = countries[i];
selectHTML += "<option value=" + country.countryCode + ">" + country.countryName + "(" + country.countryPrefix +")</option>";
}
selectHTML += "</select>";
document.getElementById("countrycode").innerHTML = selectHTML;
}
});
});
<td>
<div class="form-group">
<label class="control-label" for="countrycode"></label>
<div id="countrycode" class="col-sm-6 col-md-12"></div>
</div>
</td>
I'm getting the results. but the problem is that the Bootstrap is not not working on the select element as it's it displaying in old html form.
How could I make it to display in bootstrap manner and also how to decrease the width of select element?

Try adding the form-control class.
You can do this by changing your selectHTML line to the one below.
selectHTML="<select class='form-control'>";
and to align it properly make the following changes to div#countrycode
<div id="countrycode" class="pull-left" style="width:100%;" ></div>

Add the form-control class to the countrycode div. That should format it correctly. You can checkout the bootstrap form docs here: http://getbootstrap.com/css/#forms
To change the width, you can change the number of columns allocated to it in the class, such as changing col-sm-6 to col-sm-4.
Every time you add a nested col, it should be within a new row. Try adding the row class to your outer div
<div class="form-group row">
<label for="countrycode"></label>
<div id="countrycode" class="form-control col-sm-4"></div>
</div>

Related

Adding additional DIV input based on user input in jQuery

I'm creating function where user will insert number of value. And additional div of input will appear based the number of value of user have input previous div.
example
Insert number of user : `2`
Additional input based on number of user input
name :
name :
If user put 3 there will be 3 name and if user change it into 2 there will be 2 name.
How do I fix it in order to achieve the example that I want.
$('#user').hide();
$("#user_num").change(function()
{
$("#user").remove();
let m = $(this).val();
for (var i = 0; i < parseInt(m); i++){
$('#user').show();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div id="info">
<div class="col-lg-12 mb-30">
<label><b><span style="color:#e60000;">*</span><i>Insert number of user </i></b></label><br>
<div class="input-group-prepend">
<input class="from-control" type="number" placeholder="2" id="user_num" required >
</div>
</div>
<div class="col-lg-12 mb-30" id="user">
<label><b><span style="color:#e60000;">*</span><i> Name </i></b></label><br>
<div class="input-group-prepend">
<input class="from-control" type="text" id="name" required >
</div>
</div>
</div>
You where close to achieve it with your attempt. You needed to implement an event handler that executes a function when the input field value is changed. You can achieve this with .on('change', function(){ // do something })
Every time the function is executed it clears the html inside <div id="inputs-container"></div> and the loop appends html code to that div n times.
Try the following snippet
$('#in').on('change', function(){
let n = $(this).val()
let html_code = 'name: <input value="" style="margin-top:5px"><br>';
$('#inputs-container').html('');
for (var i = 0; i < n; i++){
$('#inputs-container').append(html_code);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="info" style="margin:10px">
Insert number of users: <input id="in" value="" />
<div id="inputs-container" style="margin-top:20px">
</div>
</div>
Note: I updated the jQuery CDN to the latest one. You where using an
older version.

Knockout. Print two properties of an object in a foreach statement

I have an observableArray property self.FilteredStudents, the students have name and lastname, and i want to print it together.
Here is the foreach code in the HTML:
<div class="row justify-content-center" data-bind="foreach: FilteredStudents">
<div class="col-md-6 col-lg-4">
<div>
<div class="LLUserCard">
<div class="LLUserCardHeader">
<div>$name $lastName</div>
<div></div>
</div>
<div class="LLUserCardBody">
</div>
<div class="LLUserCardFooter">
$enrollmentDate
</div>
</div>
</div>
</div>
</div>
Obviously the code above is not working, the pseudocode <div>$name $lastName</div> is just to specify what i am looking for.
In all the examples or tutorials i've found they use two elements like <td data-bind="text: name"></td><td data-bind="text: lastName"></td> but i don't want to print the values in different elements, i want them to be the text of one element.
Thank you.
you can use a computed or pure computed function or an inline function on the text binding https://jsfiddle.net/0o89pmju/69/
html
<p>
<span data-bind="text: Name"></span>
</p>
<p>
Or
</p>
<span data-bind="text: (firstname() + ' ' + lastname() )"></span>
js
function viewModel() {
var self = this;
this.firstname = ko.observable('bob');
this.lastname = ko.observable('smith');
self.Name = ko.pureComputed(function () {
return self.firstname() + ' ' + self.lastname()
},this);
}
var vm = new viewModel();
(function($) {
ko.applyBindings(vm); //bind the knockout model
})(jQuery);

How to display checkbox results according to my database model?

I have two variables, one stores the chexboxes and the other stores the values checked, I found a problem to display the checkboxes and their values (checked, not checked) from the controller to the view.
I have this code on the controller
$scope.infoParticipant = functionThatGetsParticipants();
for (var i = 0; i < $scope.infoParticipant.length; i++) {
if ($scope.infoParticipant[i].ch_type == 'checkbox_multiple') {
var optionsString = $scope.infoParticipant[i].cf_multiple_optionsList;
$scope.infoParticipant[i].optionsTab = optionsString.split(";");
var optionsSelected = $scope.infoParticipant[i].v_value.split(";");
}
}
In the precedent code these should be the values
optionsString = "ch1;ch2;ch3";
$scope.infoParticipant[i].v_value = "ch1;ch2";
According to this the checkboxes :ch1 and ch2 will be checked on the next view :
<div ng-show="l.ch_type=='checkbox_multiple'">
<label >My checkboxes</span></label>
<div class="row" ng-repeat="k in l.optionsTab">
<div class=" col-md-2 modal_style_input">{{k}} </div>
<div class="col-md-2"><input type="checkbox" name="option" class="md-input" id="option {{k}}" class="wizard-icheck" value="{{k}}" icheck ng-model="" /></div>
<div class="col-md-8"></div>
</div>
</div>
My question is how to modify my controller and what to put on ng-model to have my correct chexboxes checked?
Thanks in advance.
In your case, one solution would be to use ngCheck directive in your check boxes.
<input type="checkbox" id="option {{k}}" value="{{k}}" ng-check="isOptionAvailable({{k}})" /></div>
And the isOptionAvailable(opt) is a javascript function that you add to the scope which returns true when k is present in $scope.infoParticipant[i].v_value.
EDIT: To answer your last comment, I created a plunker showing how it works

API Styling Javascript + HTML

Basically I'm using bootstrap CSS with the panels / headers
I have an API going from Twitch.tv's Kraken that grabs 3 streams
My problem is it shows the 3 streams in the one box
http://jsfiddle.net/82wNq/28/
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><div id="content1"></div> - <div id="content2"></div></h3>
</div>
<div class="panel-body">
<div id="content3"></div>
</div>
</div>
If anyone could give me any pointers I'd be really greatful
You are adding all elements together in loop.
Re-Create elements & add separately.
DEMO:
http://jsfiddle.net/82wNq/30/show/
CODE:
http://jsfiddle.net/82wNq/30/
function (data) {
var temp = "";
$.each(data.streams, function (index, item) {
temp = temp + "<div class='panel-heading'><h3 class='panel-title'></h3><div id='content1'>" + item.channel.display_name + "</div><div id='content2'>" + item.viewers + "</div></div><div class='panel-body'><div id='content3'><img src='" + item.preview.medium + "'/></div></div>";
});
$("#content").html(temp); }

angularJS add a class + set default for bootstrap Tabs

I have an angular tabbable here. How can I set default tab? + add a class to specific tab?
http://plnkr.co/edit/FPMf8i
e.g. I want the tab 2 to be slected by default. Then tab 1 have a specific class.
I tried
element.children[1].addClass('test');
element[0].children[0].addClass('test');
element[0].addClass('test');
but keep getting an error
Can't use jquery has to be library already there.
Add an initial value for your ng-model, then use ng-class to set dynamic classes
Plnkr
http://plnkr.co/edit/udxAT0?p=preview
View
<div ng-controller="TabsCtrl">
<div class="tabbable" ng-model="currentTab">
<div class="tab-pane" ng-class="{'special-class': currentTab == 1}" title="1" value="1">
Content 1
</div>
<div class="tab-pane" title="2" value="2">
Content 2
</div>
</div>
</div>
Controller
var app = angular.module('plunker', ['bootstrap']);
app.controller("TabsCtrl", function($scope) {
$scope.currentTab = 1;
});