change background color of view by its ID in Titanium

Manuel_Rodrigues

I have several views that are added to a ScrollView dynamically with a for, but when the user clicks on one of the views, I want to change the Background color of the view. I have the following code for the click event:

(function() {
    var id = i;
    viewQuantity.addEventListener('click', function(e) {
        viewQuantity.backgroundColor = '#FFFFFF';
    });
})(); 

but with this code the view that changes the color is always the last added view. How can I use the id to change the view that was clicked by the user?

Rene Pot

Within a click event you get an event property in the function as first parameter. This has source object which is the element the user has clicked.

var clickedView;
(function() {
    var id = i;
    viewQuantity.addEventListener('click', function(e) {
        if (clickedView){
             clickedView.backgroundColor= '#000000'; // put your own color here to restore original
        }
        e.source.backgroundColor = '#FFFFFF';
        clickedView = e.source;
    });
})(); 

To change the color later, you can store a reference to the object, and change the color later

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Titanium Appcelerator -change border color of view dynamically

Background color not change TextEditor view

how to change background color of Qwidget irrespective of its parents background color

How to change the background color of a View Drawable without changing its body in recyclerView?

change input background color if its empty

Change element color based on background, that its on

Change text color based on its background

Change background color of a view without removing drawable

Animate change of view background color on Android

How to use animatorSet to change background color of a View

Change Recycler view item background color by code

Unable to change background color of view in SwiftUI

How to change AlertDialog view background color?

Change the background color of a Navigation View in Swift 5

Change progress tag color with its ID

How to change the label color by its id with css?

How to change background/color of the drawable set as background of a view?

Change Action Bar color to background color of recycler view item

UITableView is resetting its background color before view appears

change the background color of view when clicked on another view

How to change the background color of a tkinter Canvas after its' creation?

Find cell based on two dropdowns and change its background color

Change cell value based on its background color in google sheets

Navigation View Can't Change Its Background When It Is Selected/checked

How to change the background color of a view from a different activity in android?

How to get and change drawable background color in custom view

Background color change on table view cell tap in IOS

React Native: change background color of initial loading view

How to change background view color partialy in ios swift

TOP Ranking

HotTag

Archive