using if condition to addclass in return value using javascript/jquery?
up vote
0
down vote
favorite
I have values in an array to display in a div, when I click on a particular button, two array values will swap their positions. But I want to add different background color to the div after swaping..
My sample code is :
$(document).ready(function(){
var PlanetsModel = function() {
self.Racer = ko.observableArray([
{ id: "Racer-101", rank: "5"},
{ id: "Racer-102", rank: "2"},
{ id: "Racer-103", rank: "1"},
{ id: "Racer-104", rank: "4"},
{ id: "Racer-105", rank: "7"},
{ id: "Racer-106", rank: "8"},
{ id: "Racer-107", rank: "9"},
{ id: "Racer-108", rank: "3"},
{ id: "Racer-109", rank: "6"}
]);
console.log("First array : " + JSON.stringify(self.Racer()));
this.hideElement = function() {
function swap(data, x, y) {
var target = document.getElementById('remove_elem'),
getRacer = target.children[x];
getRacer1 = target.children[y];
console.log(getRacer);
var temp = data[x];
data[x] = data[y];
data[y] = temp;
console.log("swap values");
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#first').removeClass('sample');
$('#first').addClass('highlight');
} else {
console.log('second element is greater');
$('#first').removeClass('sample');
$('#first').addClass('remove');
}
}
swap(self.Racer(), 1,3);
self.Racer.valueHasMutated();
};
this.showRacer = ko.computed(function() {
return self.Racer();
}, this);
};
ko.applyBindings(new PlanetsModel());
});
my html code:
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span id="first" class="sample">xxx</span>
</div>
</div>
This is my sample code, it will swap values when the button is clicked. but it's adding color in the first element of the array only.
what is the problem in my code ?.
javascript jquery
add a comment |
up vote
0
down vote
favorite
I have values in an array to display in a div, when I click on a particular button, two array values will swap their positions. But I want to add different background color to the div after swaping..
My sample code is :
$(document).ready(function(){
var PlanetsModel = function() {
self.Racer = ko.observableArray([
{ id: "Racer-101", rank: "5"},
{ id: "Racer-102", rank: "2"},
{ id: "Racer-103", rank: "1"},
{ id: "Racer-104", rank: "4"},
{ id: "Racer-105", rank: "7"},
{ id: "Racer-106", rank: "8"},
{ id: "Racer-107", rank: "9"},
{ id: "Racer-108", rank: "3"},
{ id: "Racer-109", rank: "6"}
]);
console.log("First array : " + JSON.stringify(self.Racer()));
this.hideElement = function() {
function swap(data, x, y) {
var target = document.getElementById('remove_elem'),
getRacer = target.children[x];
getRacer1 = target.children[y];
console.log(getRacer);
var temp = data[x];
data[x] = data[y];
data[y] = temp;
console.log("swap values");
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#first').removeClass('sample');
$('#first').addClass('highlight');
} else {
console.log('second element is greater');
$('#first').removeClass('sample');
$('#first').addClass('remove');
}
}
swap(self.Racer(), 1,3);
self.Racer.valueHasMutated();
};
this.showRacer = ko.computed(function() {
return self.Racer();
}, this);
};
ko.applyBindings(new PlanetsModel());
});
my html code:
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span id="first" class="sample">xxx</span>
</div>
</div>
This is my sample code, it will swap values when the button is clicked. but it's adding color in the first element of the array only.
what is the problem in my code ?.
javascript jquery
Whers is your HTML? Could you please post a full exemple?
– Islam Elshobokshy
Nov 7 at 11:50
Can't do anything without your HTML markup
– Plastic
Nov 7 at 11:53
In theif..else
you're always removing / adding a class to the same element (#first
).
– Teemu
Nov 7 at 11:54
I want to remove class #first and add different class for the same div for particular swap values. How is it possible..
– NewUSer
Nov 7 at 12:02
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have values in an array to display in a div, when I click on a particular button, two array values will swap their positions. But I want to add different background color to the div after swaping..
My sample code is :
$(document).ready(function(){
var PlanetsModel = function() {
self.Racer = ko.observableArray([
{ id: "Racer-101", rank: "5"},
{ id: "Racer-102", rank: "2"},
{ id: "Racer-103", rank: "1"},
{ id: "Racer-104", rank: "4"},
{ id: "Racer-105", rank: "7"},
{ id: "Racer-106", rank: "8"},
{ id: "Racer-107", rank: "9"},
{ id: "Racer-108", rank: "3"},
{ id: "Racer-109", rank: "6"}
]);
console.log("First array : " + JSON.stringify(self.Racer()));
this.hideElement = function() {
function swap(data, x, y) {
var target = document.getElementById('remove_elem'),
getRacer = target.children[x];
getRacer1 = target.children[y];
console.log(getRacer);
var temp = data[x];
data[x] = data[y];
data[y] = temp;
console.log("swap values");
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#first').removeClass('sample');
$('#first').addClass('highlight');
} else {
console.log('second element is greater');
$('#first').removeClass('sample');
$('#first').addClass('remove');
}
}
swap(self.Racer(), 1,3);
self.Racer.valueHasMutated();
};
this.showRacer = ko.computed(function() {
return self.Racer();
}, this);
};
ko.applyBindings(new PlanetsModel());
});
my html code:
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span id="first" class="sample">xxx</span>
</div>
</div>
This is my sample code, it will swap values when the button is clicked. but it's adding color in the first element of the array only.
what is the problem in my code ?.
javascript jquery
I have values in an array to display in a div, when I click on a particular button, two array values will swap their positions. But I want to add different background color to the div after swaping..
My sample code is :
$(document).ready(function(){
var PlanetsModel = function() {
self.Racer = ko.observableArray([
{ id: "Racer-101", rank: "5"},
{ id: "Racer-102", rank: "2"},
{ id: "Racer-103", rank: "1"},
{ id: "Racer-104", rank: "4"},
{ id: "Racer-105", rank: "7"},
{ id: "Racer-106", rank: "8"},
{ id: "Racer-107", rank: "9"},
{ id: "Racer-108", rank: "3"},
{ id: "Racer-109", rank: "6"}
]);
console.log("First array : " + JSON.stringify(self.Racer()));
this.hideElement = function() {
function swap(data, x, y) {
var target = document.getElementById('remove_elem'),
getRacer = target.children[x];
getRacer1 = target.children[y];
console.log(getRacer);
var temp = data[x];
data[x] = data[y];
data[y] = temp;
console.log("swap values");
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#first').removeClass('sample');
$('#first').addClass('highlight');
} else {
console.log('second element is greater');
$('#first').removeClass('sample');
$('#first').addClass('remove');
}
}
swap(self.Racer(), 1,3);
self.Racer.valueHasMutated();
};
this.showRacer = ko.computed(function() {
return self.Racer();
}, this);
};
ko.applyBindings(new PlanetsModel());
});
my html code:
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span id="first" class="sample">xxx</span>
</div>
</div>
This is my sample code, it will swap values when the button is clicked. but it's adding color in the first element of the array only.
what is the problem in my code ?.
javascript jquery
javascript jquery
edited Nov 7 at 12:04
asked Nov 7 at 11:49
NewUSer
33
33
Whers is your HTML? Could you please post a full exemple?
– Islam Elshobokshy
Nov 7 at 11:50
Can't do anything without your HTML markup
– Plastic
Nov 7 at 11:53
In theif..else
you're always removing / adding a class to the same element (#first
).
– Teemu
Nov 7 at 11:54
I want to remove class #first and add different class for the same div for particular swap values. How is it possible..
– NewUSer
Nov 7 at 12:02
add a comment |
Whers is your HTML? Could you please post a full exemple?
– Islam Elshobokshy
Nov 7 at 11:50
Can't do anything without your HTML markup
– Plastic
Nov 7 at 11:53
In theif..else
you're always removing / adding a class to the same element (#first
).
– Teemu
Nov 7 at 11:54
I want to remove class #first and add different class for the same div for particular swap values. How is it possible..
– NewUSer
Nov 7 at 12:02
Whers is your HTML? Could you please post a full exemple?
– Islam Elshobokshy
Nov 7 at 11:50
Whers is your HTML? Could you please post a full exemple?
– Islam Elshobokshy
Nov 7 at 11:50
Can't do anything without your HTML markup
– Plastic
Nov 7 at 11:53
Can't do anything without your HTML markup
– Plastic
Nov 7 at 11:53
In the
if..else
you're always removing / adding a class to the same element (#first
).– Teemu
Nov 7 at 11:54
In the
if..else
you're always removing / adding a class to the same element (#first
).– Teemu
Nov 7 at 11:54
I want to remove class #first and add different class for the same div for particular swap values. How is it possible..
– NewUSer
Nov 7 at 12:02
I want to remove class #first and add different class for the same div for particular swap values. How is it possible..
– NewUSer
Nov 7 at 12:02
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You are always changing the value of the div with id first
.
id's have to be unique and so none of the other ones with the id of first
are changing because jquery stops searching for others after it finds the first element you're looking for.
You should add something to the id that makes it unique to that racer, like making the id of the div the same as the id of the racer. for instance: data-bind='attr: { "id": "id"}
(where the second id is the id of the racer object).
then you reference it again in your javascript by calling the same id related to that racer object like $("#"+racer[x].id)
Updating your code it should look like this, if I'm understanding your use of data-binding code correctly.
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span data-bind='attr: {"id": "id" }' class="sample">xxx</span>
</div>
</div>
and javascript can be updated like this:
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#'+self.Racer()[x].id).removeClass('sample');
$('#'+self.Racer()[x].id).addClass('highlight');
} else {
console.log('second element is greater');
$('#'+self.Racer()[y].id).removeClass('sample');
$('#'+self.Racer()[y].id).addClass('remove');
}
I add $('#first'+self.Racer()[y].rank) in my code, there is no other color change in the rows..
– NewUSer
Nov 7 at 12:09
@NewUSer I've updated my answer a little, try the thing with the id and check in the html that is rendered by inspecting it with your browsers debugger to make sure that the id value is getting set as expected as I'm unsure how your data binding library works so my example might not be 100% correct in terms of syntax
– topher
Nov 7 at 12:12
I should remove quotes in id data-bind='attr: {"id": id }', it should perform change color on one value.. I want to change color on both swapped values..
– NewUSer
Nov 7 at 12:25
Then I guess it's just down to what you have in yourif
andelse
blocks. You need to add the class to change the highlighting to each div you want the highlighting changed on, but you'll have$('#'+self.Racer()[x].id)
and$('#'+self.Racer()[y].id)
in the `if block where you change their classes to whatever it is that highlights them. I'm having a little trouble understanding your english and what you actually want in these comments now though.
– topher
Nov 7 at 12:29
this example will works correctly. I will add only one class in only if and else statement. there is the problem in my code. I will add both two class in $('#'+self.Racer()[x].id) and $('#'+self.Racer()[y].id) in if and else statement. They should perform correctly
– NewUSer
Nov 8 at 6:08
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You are always changing the value of the div with id first
.
id's have to be unique and so none of the other ones with the id of first
are changing because jquery stops searching for others after it finds the first element you're looking for.
You should add something to the id that makes it unique to that racer, like making the id of the div the same as the id of the racer. for instance: data-bind='attr: { "id": "id"}
(where the second id is the id of the racer object).
then you reference it again in your javascript by calling the same id related to that racer object like $("#"+racer[x].id)
Updating your code it should look like this, if I'm understanding your use of data-binding code correctly.
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span data-bind='attr: {"id": "id" }' class="sample">xxx</span>
</div>
</div>
and javascript can be updated like this:
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#'+self.Racer()[x].id).removeClass('sample');
$('#'+self.Racer()[x].id).addClass('highlight');
} else {
console.log('second element is greater');
$('#'+self.Racer()[y].id).removeClass('sample');
$('#'+self.Racer()[y].id).addClass('remove');
}
I add $('#first'+self.Racer()[y].rank) in my code, there is no other color change in the rows..
– NewUSer
Nov 7 at 12:09
@NewUSer I've updated my answer a little, try the thing with the id and check in the html that is rendered by inspecting it with your browsers debugger to make sure that the id value is getting set as expected as I'm unsure how your data binding library works so my example might not be 100% correct in terms of syntax
– topher
Nov 7 at 12:12
I should remove quotes in id data-bind='attr: {"id": id }', it should perform change color on one value.. I want to change color on both swapped values..
– NewUSer
Nov 7 at 12:25
Then I guess it's just down to what you have in yourif
andelse
blocks. You need to add the class to change the highlighting to each div you want the highlighting changed on, but you'll have$('#'+self.Racer()[x].id)
and$('#'+self.Racer()[y].id)
in the `if block where you change their classes to whatever it is that highlights them. I'm having a little trouble understanding your english and what you actually want in these comments now though.
– topher
Nov 7 at 12:29
this example will works correctly. I will add only one class in only if and else statement. there is the problem in my code. I will add both two class in $('#'+self.Racer()[x].id) and $('#'+self.Racer()[y].id) in if and else statement. They should perform correctly
– NewUSer
Nov 8 at 6:08
|
show 1 more comment
up vote
0
down vote
accepted
You are always changing the value of the div with id first
.
id's have to be unique and so none of the other ones with the id of first
are changing because jquery stops searching for others after it finds the first element you're looking for.
You should add something to the id that makes it unique to that racer, like making the id of the div the same as the id of the racer. for instance: data-bind='attr: { "id": "id"}
(where the second id is the id of the racer object).
then you reference it again in your javascript by calling the same id related to that racer object like $("#"+racer[x].id)
Updating your code it should look like this, if I'm understanding your use of data-binding code correctly.
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span data-bind='attr: {"id": "id" }' class="sample">xxx</span>
</div>
</div>
and javascript can be updated like this:
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#'+self.Racer()[x].id).removeClass('sample');
$('#'+self.Racer()[x].id).addClass('highlight');
} else {
console.log('second element is greater');
$('#'+self.Racer()[y].id).removeClass('sample');
$('#'+self.Racer()[y].id).addClass('remove');
}
I add $('#first'+self.Racer()[y].rank) in my code, there is no other color change in the rows..
– NewUSer
Nov 7 at 12:09
@NewUSer I've updated my answer a little, try the thing with the id and check in the html that is rendered by inspecting it with your browsers debugger to make sure that the id value is getting set as expected as I'm unsure how your data binding library works so my example might not be 100% correct in terms of syntax
– topher
Nov 7 at 12:12
I should remove quotes in id data-bind='attr: {"id": id }', it should perform change color on one value.. I want to change color on both swapped values..
– NewUSer
Nov 7 at 12:25
Then I guess it's just down to what you have in yourif
andelse
blocks. You need to add the class to change the highlighting to each div you want the highlighting changed on, but you'll have$('#'+self.Racer()[x].id)
and$('#'+self.Racer()[y].id)
in the `if block where you change their classes to whatever it is that highlights them. I'm having a little trouble understanding your english and what you actually want in these comments now though.
– topher
Nov 7 at 12:29
this example will works correctly. I will add only one class in only if and else statement. there is the problem in my code. I will add both two class in $('#'+self.Racer()[x].id) and $('#'+self.Racer()[y].id) in if and else statement. They should perform correctly
– NewUSer
Nov 8 at 6:08
|
show 1 more comment
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You are always changing the value of the div with id first
.
id's have to be unique and so none of the other ones with the id of first
are changing because jquery stops searching for others after it finds the first element you're looking for.
You should add something to the id that makes it unique to that racer, like making the id of the div the same as the id of the racer. for instance: data-bind='attr: { "id": "id"}
(where the second id is the id of the racer object).
then you reference it again in your javascript by calling the same id related to that racer object like $("#"+racer[x].id)
Updating your code it should look like this, if I'm understanding your use of data-binding code correctly.
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span data-bind='attr: {"id": "id" }' class="sample">xxx</span>
</div>
</div>
and javascript can be updated like this:
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#'+self.Racer()[x].id).removeClass('sample');
$('#'+self.Racer()[x].id).addClass('highlight');
} else {
console.log('second element is greater');
$('#'+self.Racer()[y].id).removeClass('sample');
$('#'+self.Racer()[y].id).addClass('remove');
}
You are always changing the value of the div with id first
.
id's have to be unique and so none of the other ones with the id of first
are changing because jquery stops searching for others after it finds the first element you're looking for.
You should add something to the id that makes it unique to that racer, like making the id of the div the same as the id of the racer. for instance: data-bind='attr: { "id": "id"}
(where the second id is the id of the racer object).
then you reference it again in your javascript by calling the same id related to that racer object like $("#"+racer[x].id)
Updating your code it should look like this, if I'm understanding your use of data-binding code correctly.
<button id='swap' data-bind='click: hideElement'>Swap Values</button>
<div id='remove_elem' data-bind='template: { foreach: showRacer }'>
<div style="width: 50%;" data-bind='attr: { "class": "planet "}'>
<div class="left" data-bind='text: id'></div>
<div class="right" data-bind='text: rank'></div>
<span data-bind='attr: {"id": "id" }' class="sample">xxx</span>
</div>
</div>
and javascript can be updated like this:
if(self.Racer()[x].rank > self.Racer()[y].rank) {
console.log('first element is greater');
$('#'+self.Racer()[x].id).removeClass('sample');
$('#'+self.Racer()[x].id).addClass('highlight');
} else {
console.log('second element is greater');
$('#'+self.Racer()[y].id).removeClass('sample');
$('#'+self.Racer()[y].id).addClass('remove');
}
edited Nov 7 at 12:10
answered Nov 7 at 11:59
topher
393212
393212
I add $('#first'+self.Racer()[y].rank) in my code, there is no other color change in the rows..
– NewUSer
Nov 7 at 12:09
@NewUSer I've updated my answer a little, try the thing with the id and check in the html that is rendered by inspecting it with your browsers debugger to make sure that the id value is getting set as expected as I'm unsure how your data binding library works so my example might not be 100% correct in terms of syntax
– topher
Nov 7 at 12:12
I should remove quotes in id data-bind='attr: {"id": id }', it should perform change color on one value.. I want to change color on both swapped values..
– NewUSer
Nov 7 at 12:25
Then I guess it's just down to what you have in yourif
andelse
blocks. You need to add the class to change the highlighting to each div you want the highlighting changed on, but you'll have$('#'+self.Racer()[x].id)
and$('#'+self.Racer()[y].id)
in the `if block where you change their classes to whatever it is that highlights them. I'm having a little trouble understanding your english and what you actually want in these comments now though.
– topher
Nov 7 at 12:29
this example will works correctly. I will add only one class in only if and else statement. there is the problem in my code. I will add both two class in $('#'+self.Racer()[x].id) and $('#'+self.Racer()[y].id) in if and else statement. They should perform correctly
– NewUSer
Nov 8 at 6:08
|
show 1 more comment
I add $('#first'+self.Racer()[y].rank) in my code, there is no other color change in the rows..
– NewUSer
Nov 7 at 12:09
@NewUSer I've updated my answer a little, try the thing with the id and check in the html that is rendered by inspecting it with your browsers debugger to make sure that the id value is getting set as expected as I'm unsure how your data binding library works so my example might not be 100% correct in terms of syntax
– topher
Nov 7 at 12:12
I should remove quotes in id data-bind='attr: {"id": id }', it should perform change color on one value.. I want to change color on both swapped values..
– NewUSer
Nov 7 at 12:25
Then I guess it's just down to what you have in yourif
andelse
blocks. You need to add the class to change the highlighting to each div you want the highlighting changed on, but you'll have$('#'+self.Racer()[x].id)
and$('#'+self.Racer()[y].id)
in the `if block where you change their classes to whatever it is that highlights them. I'm having a little trouble understanding your english and what you actually want in these comments now though.
– topher
Nov 7 at 12:29
this example will works correctly. I will add only one class in only if and else statement. there is the problem in my code. I will add both two class in $('#'+self.Racer()[x].id) and $('#'+self.Racer()[y].id) in if and else statement. They should perform correctly
– NewUSer
Nov 8 at 6:08
I add $('#first'+self.Racer()[y].rank) in my code, there is no other color change in the rows..
– NewUSer
Nov 7 at 12:09
I add $('#first'+self.Racer()[y].rank) in my code, there is no other color change in the rows..
– NewUSer
Nov 7 at 12:09
@NewUSer I've updated my answer a little, try the thing with the id and check in the html that is rendered by inspecting it with your browsers debugger to make sure that the id value is getting set as expected as I'm unsure how your data binding library works so my example might not be 100% correct in terms of syntax
– topher
Nov 7 at 12:12
@NewUSer I've updated my answer a little, try the thing with the id and check in the html that is rendered by inspecting it with your browsers debugger to make sure that the id value is getting set as expected as I'm unsure how your data binding library works so my example might not be 100% correct in terms of syntax
– topher
Nov 7 at 12:12
I should remove quotes in id data-bind='attr: {"id": id }', it should perform change color on one value.. I want to change color on both swapped values..
– NewUSer
Nov 7 at 12:25
I should remove quotes in id data-bind='attr: {"id": id }', it should perform change color on one value.. I want to change color on both swapped values..
– NewUSer
Nov 7 at 12:25
Then I guess it's just down to what you have in your
if
and else
blocks. You need to add the class to change the highlighting to each div you want the highlighting changed on, but you'll have $('#'+self.Racer()[x].id)
and $('#'+self.Racer()[y].id)
in the `if block where you change their classes to whatever it is that highlights them. I'm having a little trouble understanding your english and what you actually want in these comments now though.– topher
Nov 7 at 12:29
Then I guess it's just down to what you have in your
if
and else
blocks. You need to add the class to change the highlighting to each div you want the highlighting changed on, but you'll have $('#'+self.Racer()[x].id)
and $('#'+self.Racer()[y].id)
in the `if block where you change their classes to whatever it is that highlights them. I'm having a little trouble understanding your english and what you actually want in these comments now though.– topher
Nov 7 at 12:29
this example will works correctly. I will add only one class in only if and else statement. there is the problem in my code. I will add both two class in $('#'+self.Racer()[x].id) and $('#'+self.Racer()[y].id) in if and else statement. They should perform correctly
– NewUSer
Nov 8 at 6:08
this example will works correctly. I will add only one class in only if and else statement. there is the problem in my code. I will add both two class in $('#'+self.Racer()[x].id) and $('#'+self.Racer()[y].id) in if and else statement. They should perform correctly
– NewUSer
Nov 8 at 6:08
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188877%2fusing-if-condition-to-addclass-in-return-value-using-javascript-jquery%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Whers is your HTML? Could you please post a full exemple?
– Islam Elshobokshy
Nov 7 at 11:50
Can't do anything without your HTML markup
– Plastic
Nov 7 at 11:53
In the
if..else
you're always removing / adding a class to the same element (#first
).– Teemu
Nov 7 at 11:54
I want to remove class #first and add different class for the same div for particular swap values. How is it possible..
– NewUSer
Nov 7 at 12:02