Help JavaScript addClass not working with if Else.

This forum contains topics that were moved from Get Satisfaction
eric okeson
Posts: 0
Joined: Mon Sep 29, 2014 12:33 am

Help JavaScript addClass not working with if Else.

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Help JavaScript addClass not working with if Else.

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

eric okeson
Posts: 0
Joined: Mon Sep 29, 2014 12:33 am

Help JavaScript addClass not working with if Else.

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Help JavaScript addClass not working with if Else.

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

Post Reply