Couldn't change the text between a Html element using JQuery - html

I have the JQuery code below. I changed the text between a button element from + to x in second if statement. Now, when the button is displaying x, I wanted to change the text to + in first if statement using $(this).text("+");, yet the button is displaying x even after the change. I have checked if the text was changed to + using console.log() and it says it has changed to +. But, why isn't the result showing its effect on the button?
let players = [];
let remainingPlayers = 11;
$(".card > button").click(function(e) {
let removePlayer = "";
let index = -1;
if ($(this).text() !== "+") {
removePlayer = $(this).attr("value");
index = players.indexOf(removePlayer);
if (index != -1) {
$(this).text("+");
players.splice(index, 1);
console.log(players);
console.log($(this).text());
}
}
if (remainingPlayers != 0) {
remainingPlayers -= 1;
players.push($(this).attr("value"));
$(this).text("x");
$(this).addClass("btn btn-danger button-select-players");
$("#pl-remain").text(remainingPlayers);
} else {
alert("You can't select more players");
console.log(players);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-lg-6 col-md-12 col-sm-12">
<div class="card" style="width: 15rem;">
<img src="..." class="card-img-top" alt="...">
<button class="btn btn-success button-select-players" type="button" name="button" value="xyz">+</button>
<ul class="list-group list-group-flush">
<li class="first-li list-group-item">Name</li>
<li class="list-group-item">0</li>
</ul>
</div>
</div>

Related

Delete an item(Object) from an array using ng-if

I am trying to remove item using ng-if and ng-else-if.
"ng-if="navItem.gbn.indexOf('12345') !== -1" is true then array item only show contains('12345') items.
but the item still remain in array so I think ng-else-if is not working.
I don't know how to delete it.
I have the following Angular code:
<div ng-controller="PIDAEController">
<div class="container-fluid" style="padding-top:30px;">
<div class="row">
<div class="col-lg-2 col-md-3 col-xs-6 tubmbimg" ng-repeat="navItem in navItems | Fpage: curPage * pageSize | limitTo:pageSize" ng-if="navItem.gbn.indexOf('12345') !== -1">
<a class="thumbnail border" href="#/item/{{navItem.site}}">
<div class="b-marjin">
<span align="center" class="label label-primary full-width" >{{ navItem.gbn }}</span>
</div>
<div>
<img style="border: 1px solid black;" class="img-responsive full-width" src="{{navItem.pimg}}" alt="">
</div>
<div align="center" class="under-block">
<span title="{{ navItem.pname }}" align="center">{{ navItem.pname2}}</span>
</div>
</a>
</div>
<div ng-else-if="navItems.splice(navItem, 1)"></div>
</div>
</div>
</div>
angular.forEach(item, function (value, key) {
var keepGoing = true;
if(keepGoing) {
if(value.gbn.indexOf("11600") != -1) {
$scope.initialized = true;
$scope.navItems = item;
$scope.curPage=0;
$scope.pageSize=20;
$scope.numberOfPage = Math.ceil(item.length / $scope.pageSize);
keepGoing = false;
}
else {
$scope.navItems.splice(key, 1);
}
}
});
ng-else-if does NOT EXIST. its neither angular or angularJS.
In the line
<div ng-else-if="navItems.splice(navItem, 1)"></div>
splice requires navItem to be an index/int, not an object.
As an alternative to ng-else-if you can simply do:
ng-if="navItem.gbn.indexOf('12345') == -1
Also, splice would return the removed array item when found, not sure if "ng-if" handles that as being TRUE.

Getting the ERROR TypeError: Cannot read property 'completed' of undefined

I am currently building a To Do list and adding a checkbox that has a click event, once the checkbox has been clicked on, it should have a strikethrough element on the task that was entered in.
HTML Code:
<ul class="list-group">
<li *ngFor="let todo of todos; let i = index"
class="list-group-item shadow p-3 mb-5 bg-white rounded border border-dark rounded" id="myTask">
<div class="todo-item">
<span class="todo-title" [ngClass]="{'todo-complete': todo.completed}">
{{todo.task}} <input type="checkbox" class="todo-checkbox" (click)="completeItem()" />
<button type="button" class="btn btn-danger" (click)="delete()">X</button>
</span>
</div>
</li>
</ul>
TypeScript Code:
addToDo(todo: string, cm?: boolean) {
const td = {
id: null,
task: todo,
completed: cm,
}
if (todo === '') {
alert('You must enter in a task TO DO!')
} else {
this.todos.push(td);
}
this.saveItemsToLocalStorage(this.todos);
}
delete(index: number) {
this.todos.splice(index, 1);
console.log("index", index);
this.saveItemsToLocalStorage(this.todos);
}
clear() {
this.todos = [];
console.log('index', this.todos)
this.saveItemsToLocalStorage(this.todos);
}
completeItem() {
this.update.emit({
todo: this.todos,
changes: {completed: !this.todo.completed},
});
}

How to create rows dynamically with razor

I'm trying to create rows dynamically with razor but I can't figure out how to wrap this around. What I want is for every 5:e object in my model I want to create a new row/div <div class="row pics_in_a_row"> so that each row contains five or less images.
<section class="slice">
<div class="wp-section">
<div class="container">
<div class="row pics_in_a_row">
#{
int i = 0;
foreach (dbphoto.Models.Image s in Model.images.OrderByDescending(x => x.idimage))
{
if (i % 5 == 0 && i != 0)
{
<br />
}
i++;
if (1 == 1)
{
<div class="flxbox" style="flex: calc(1024/713)">
<a href="#s.HighResolution" data-fancybox="gallery" data-caption="xyz, Stockholm">
<img src="#s.LowResolution" class="img-fluid rounded flximg" />
</a>
</div>
}
}
}
</div>
</div>
</div>
</section>
Bonus question: how can I get the height and width of the image and add it to the calc() css?
You should add the div with class row pics_in_a_row inside your loop
This should create a a row for every 5 items (or less for the last row when the remaining items are less than 5).
<div class="container">
#{
var numberOfColsNeeded = 5;
var totalCounter = Model.images.Count();
var itemCounter = 1;
}
#foreach (var item in Model.images)
{
if (itemCounter % numberOfColsNeeded == 1)
{
#:<div class="row pics_in_a_row">
}
<div class="flxbox" style="flex: calc(1024 / 713)">
<img src="#item.LowResolution" class="img-fluid rounded flximg" />
</div>
if ((itemCounter % numberOfColsNeeded == 0) || ((itemCounter) == totalCounter))
{
#:</div>
}
itemCounter++;
}
</div>

Formatting using loop

Currently at the moment I am able display text from a loop and above the text a red circle displays using the i class and code below:
<div id="testwrap">
#foreach (mainnav mainnav in #Model)
{
if (mainnav.HasSideNav == 0)
{
<a style="color:red" href='#Url.Action("Content", new { id = mainnav.contentID })' class="ajax2">#mainnav.DisplayLabel</a>
}
else
{
<div class="col-sm-3 text-center feature">
<i class="fa fa-tablet iconred"></i>
<a style="color:blue" href='#Url.Action("SideNavLevel1", new { id = mainnav.MNavSubID })' class="ajax2">#mainnav.DisplayLabel</a>
</div>
}
}
</div>
My aim is it to have the icon be blue, green, yellow, orange etc...
I have tried to add in a counter but it doesn't work.
Can anybody help me to add the loop/ counter in to change where is says iconred to iconblue etc...?
You can keep your colors in an array and use that along with a counter.
#{
var counter = 0;
string[] colors = new string[] { "red", "blue", "orange","green" };
}
<div id="testwrap">
#foreach (var mainnav in YourCollection)
{
if (mainnav.HasSideNav == 0)
{
<a style="color: red" href='#' class="ajax2">#mainnav.DisplayLabel</a>
}
else
{
<div class="col-sm-3 text-center feature">
<i class="fa fa-tablet icon#(colors[counter])"></i>
<a style="color: blue" href='#' class="ajax2">#mainnav.DisplayLabel</a>
</div>
}
counter++;
if (counter == 4)
{
counter = 0;
}
}
</div>
If you have more than 4 items in your collection, the fifth item will be again red and so on.

Reload a div tag instead of a page reload using angularjs

I have the below div tag which contains a dropdown and a carousel. Whenever I select from the dropdown I call a function in an AngularJS controller which calls golang at the backend using http.golang to generate the data to be displayed in the carousel and sends a response which is displayed in the carousel but creates a page refresh. I just want this div to be loaded again and not the entire page to load. How can I do this using AngularJS?
HTML:
<div class="col-xs-11" style="width:750pt; height:180pt; margin-top:30px; ">
<div class="row " style="margin-left:75pt;">
<div class="dropdown">
<label class="text-center" style="margin-top:10pt;font-size:14pt;"> Deals on</label>
<button data-toggle="dropdown" class=" btn dropdown-toggle dropdown-menu-hover-color text-center" style="padding:0px 0px;background-color:white;" href="#"><label style="font-size:14pt; color: #191970;" >{{currentCategory}}</label> <b class="caret"></b></button>
<ul class="dropdown-menu submenu-hover-color dropdown-menu-scroll" style="margin:0;">
<li>{{page.category_name}}</li>
</ul>
<div style="display: inline;" ng-hide="deals"> <label class="text-center" style="margin-top:10pt;font-size:14pt;color: #191970;;">No deals found, please select a different category. </label></div>
</div>
</div>
<div id ="carousel_category" class="carousel slide " ng-show ="deals">
<div class="carousel-inner text-center" style="margin-left:75px;">
<div class="item active">
<div class="carousel-width" ng-repeat="(key, value) in categoryList.slice(0, 5) track by $index">
<img ng-src= {{value.imageUrl}} alt="No Image"/>
<h6>{{value.productName}}</h6>
<b style="color:#990000;">{{value.price}}</b> <b style="color:green;">{{value.discount}}</b>
</div>
</div>
<div class="item" ng-repeat="page in category_products_pages">
<div class="carousel-width" ng-repeat="image in page">
<img ng-src= {{image.imageUrl}} alt="No Image" />
<h6>{{image.productName}}</h6>
<b style="color:#990000;">{{image.price}}</b> <b style="color:green;">{{image.discount}}</b>
</div>
</div>
</div> <!--End of carousel-inner-->
<a class="left carousel-control pull-left" href="#carousel_category" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
<a class="right carousel-control pull-right" href="#carousel_category" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
</div><!--End of carousel_cartegory-->
</div>
Angularjs :(Below code is inside angularjs controller)
$scope.changeCategory = function(category_id) {
$http({
method : 'POST',
url : '/changeCategory',
data : {"category_id" : category_id},
headers : { 'Content-Type': 'application/x-www-form-urlencoded' }
// set the headers so angular passing info as form data (not request payload)
}).success(function(data, status, headers, config) {
$scope.deals = true;
$scope.products = JSON.parse(JSON.stringify(data));
$scope.currentCategory = $scope.products.categoryInfo.category_name;
$scope.dropdownCategoryList = $scope.products.dropdownCategoryList;
$scope.categoryList = $scope.products.categoryList;
if($scope.categoryList == undefined) {
$scope.deals = false;
return;
}
currList = $scope.categoryList;
for(var i=0;i<currList.length;i++){
product_type = currList[i].product_type;
if (product_type != "Price_With_Discount") {
currList[i].price = "";
currList[i].discount = "Save Upto "+currList[i].discount + "%";
} else {
currList[i].price = "$" + currList[i].price;
currList[i].discount = "(Save "+currList[i].discount+"%)"
}
}
//category_data
var category_products_page = [];
var category_products_pages = [];
var count_1 = 5;
$scope.category_products_pages = category_products_pages;
for(var j=5;j<$scope.categoryList.length;j++){
if(count_1 > 1){
category_products_page.push($scope.categoryList[j]);
count_1--;
if($scope.categoryList.length === j+1){
category_products_pages.push(category_products_page);
//return;
break;
}
}else{
category_products_page.push($scope.categoryList[j]);
category_products_pages.push(category_products_page);
category_products_page = [];
count_1 = 5;
}
}
}).error(function(data, status, headers, config) {
$scope.status = status;
$window.alert("error")
});
}
});