PDA

View Full Version : Applying styles with DomHelper & DomQuery



bjudson
14 Sep 2011, 7:33 PM
I'm trying to do some simple style application with Sencha Touch. Specifically, I would think that this would make the image with id = "piece-5" opaque:

Ext.DomHelper.applyStyles(Ext.DomQuery.selectNode('img#piece-5'),'opacity: 1');

But it doesn't. DomQuery seems to be passing in an object that is not connected to the actual DOM element.

What am I doing wrong here?

AndreaCammarata
15 Sep 2011, 1:14 AM
I think that the main problem with this is that you are trying to get an element that has still not be rendered on the page.
Are you trying to get that image just after showing the container view?

bjudson
15 Sep 2011, 6:11 AM
Thanks for your response. I have already manipulated the image (making it transparent in the first place) by the time I'm calling this line, so it has already been rendered. Here's some more context.

I have a panel with some images in <li> elements. The <li>s have background images set on them, so making the contained img transparent (opacity: 0) reveals the background image. This is working fine. Now I'm trying to 'hide' the background image by setting the opacity of the img element back to 1.
The panel has this listener:



listeners: {
el: {
tap: this.imgFlip,
delegate: 'img'
}
}


Which calls this function:



imgFlip: function (item) {
if(typeof this.flipped == 'undefined'){
this.flipped = new Array();
}

Ext.DomHelper.applyStyles(item.target, 'opacity: 0');

this.flipped.push(item.target.id);

if(this.flipped.length > 1){
console.log(this.flipped);
for(var i=0; i<this.flipped.length; i++){
var el = Ext.DomQuery.selectNode('img#' + this.flipped[i]);
Ext.DomHelper.applyStyles(el, 'opacity: 1');
console.log(el);
}
this.flipped = [];
}
},


At this point, the idea is just to allow the user to tap one image to 'reveal' what is under it, and then when they tap another image it briefly hides the second one and then shows both again.
After clicking 2 items, I see this in the console:



["piece-6","piece-5"]
<img src=?"img/?jadelogo.jpg" style=?"width:? 80px;? opacity:? 1;? " id=?"piece-6"class=?"artifact-2">?
<img src=?"img/?jadelogo.jpg" style=?"width:? 80px;? opacity:? 1;? " id=?"piece-5"class=?"artifact-1">?


But the images remain transparent, and I if I view them in the DOM, I see:



<img src="img/jadelogo.jpg" style="width:80px;opacity:0;" id="piece-6" class="artifact-2">


I hope this clarifies the question.