Page 1 of 1

Help JavaScript addClass not working with if Else.

Posted: Fri Oct 17, 2014 7:37 am
by eric okeson

Help JavaScript addClass not working with if Else.
I have a working example http://jsfiddle.net/ericokeson/vbL8mL...
I've changed $('li') to be Appery('raw_html') but the script isn't being applied.
Ive tested a simpler script. Appery('raw_html').addClass("good"); and it works perfect.
Is there something in my script that is not compatible with Appery? Is there another method of accomplishing this?


Help JavaScript addClass not working with if Else.

Posted: Fri Oct 17, 2014 9:23 am
by Kateryna Grynko

Hi Eric,

Are you able to get an element by selector?
Please clarify when you try to add a class.
Probably you use page Load instead of Page Show event.


Help JavaScript addClass not working with if Else.

Posted: Fri Oct 17, 2014 8:30 pm
by eric okeson

Yes I can get an element by selector. This example worked for me.

var myList = document.getElementsByName("raw_html");
myList[0].style.color = "red";

The script is run as an event after clicking a button. The first event calls data from an API and places it in an HTML box, the second event changes the css depending on what data is returned.


Help JavaScript addClass not working with if Else.

Posted: Mon Oct 20, 2014 12:21 am
by Yurii Orishchuk

HI Eric,

Your code is correct.

And you should use it in Appery like in JS fiddle with minor changes:

pre

//Change selector.
$('[name="raw_html"]').each(

function () {

Code: Select all

 //Add trim to clear all whitespaces. 
 var score = $(this).text().trim(); 

 //Debug information. Take a look in console. 
 console.log("score = " + score); 

 if (score == "protein") { 
     $(this).addClass('good'); 
 } else if (score == "25") { 
     $(this).addClass('good'); 
 } else if (score == "20") { 
     $(this).addClass('nut'); 
 } else if (score == "15") { 
     $(this).addClass('bad'); 
 } else if (score == "10") { 
     $(this).addClass('bad'); 
 } 

});

/pre

Regards.