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 ?.










share|improve this question
























  • 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















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 ?.










share|improve this question
























  • 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













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 ?.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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
















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












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');
}





share|improve this answer























  • 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 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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');
}





share|improve this answer























  • 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 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















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');
}





share|improve this answer























  • 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 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













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');
}





share|improve this answer














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');
}






share|improve this answer














share|improve this answer



share|improve this answer








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 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


















  • 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 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()