John5269257
Posts: 0
Joined: Mon Jun 17, 2013 11:04 am

adding a class to a mapped variable

I expect this is simple, but I've been trying different approaches for hours.

I have a simple mapping of a service response to a field, and I want to add a class to the field if the value returned is 0

In the field's javascript I've tried things like:

if (value 0){
$(this).addClass("newclass")
}

and I've tried element.name and all sorts of other guesses, but whatever I do I can't get a handle on the element to pass to jquery to add the class.

Please put me out of my misery - how do I pass an element to jquery?
Thanks
John

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

adding a class to a mapped variable

Hello,

You can use localStorage variable and depends on localStorage variable value make some events.

John5269257
Posts: 0
Joined: Mon Jun 17, 2013 11:04 am

adding a class to a mapped variable

Thanks Igor
I think that means I'd have to have a variable for each field - I might as well just use something like:

if (value 0){
$("[dsid='myfield1']").addClass("newclass")
}

in each field.

I was hoping for a general case so I could pass the element to a function, not write a different handler for each field

John

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

adding a class to a mapped variable

John,

Please provide us more description what exactly do you want to do.

John5269257
Posts: 0
Joined: Mon Jun 17, 2013 11:04 am

adding a class to a mapped variable

Igor,

I have a start screen that queries several services that return counts of different types of message. So this screen will show:

count1 messagetype1
count2 messagetype2
count3 messagetype3
...
countn messagetypen

If any of the counts are zero I want to visually show this on the count - perhaps change the background color. So I want to add a classname to any of the counts that are 0.

So for each of the mapped count fields, in their javascript, I want to use jquery to add a classname.

So in the javascript for each mapped field I'm trying variations on this approach:

if (value 0){
$(this).addClass("newclass")
}

But I can't get a handle on the field.

What does work is this approach:

if (value 0){
$('[dsid="countn"]').addClass("foo")
}

But that means I have to hardcode each handler with the name of the field.

This may seem trivial, but I'm bumping in to this sort of problem a lot, and I'm sure there is an elegant way to do it.

Thanks
John

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

adding a class to a mapped variable

Please post a screenshot of mapping.

John5269257
Posts: 0
Joined: Mon Jun 17, 2013 11:04 am

adding a class to a mapped variable

sorry about the delay in responding.
The mapping couldn't be simpler:
Image
I have other services mapped to other fields: taskcount and messagecount receive a number from other services.

What I want to achieve:
A simple way for all the count fields to check if they are greater than zero then call jquery on themselves to add a class or otherwise change their appearance. The sort of thing that I'm looking for, in the javascript of each mapped field, would be:

if (value 0){
$(this).addClass("newclass")
}

but this doesn't work, and i can't figure out how to pass the element to javascript, without hardcoding each field like so:

if (value 0){
$('[dsid="messagecount"]').addClass("newclass")
}

I bet there's a simple solution to this, and I will kick myself.
John

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

adding a class to a mapped variable

Hello! Try
codeif (+value > 0){
element.addClass("newclass") ;
}/code

John5269257
Posts: 0
Joined: Mon Jun 17, 2013 11:04 am

adding a class to a mapped variable

It works :D

But I don't know how! Is "element" a jQuery object?

Fantastic - thanks Marina

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

adding a class to a mapped variable

Yes, it's a jQuery object. "element" is a parameter which is transmitted to mapping function and it indicates just created object.

Return to “Issues”