Problem
dojo selector – “dojo.query” doesn’t work for elements generated by xpages
Background
This is a follow on to the jQuery selector function for xPages – x$(“#{id:inputText1}”) article I wrote previously.
Solution
Turns out that dojo.query suffers from the same problem: dojo.query cannot understand elements with colons (:) in it. We have to convert the idTag of the element to escape the colons with a backslash.
- dojo.query(“view:_id1:inputText1”).style(“border”, “1px solid red”) – will not work
- dojo.query(“view\:_id1\:inputText1”).style(“border”, “1px solid red”) – will work and is converted using the function
So I have updated my x$ function to cover dojo and jQuery
function x$(idTag, param, jd){ //Updated 28 Feb 2012 idTag=idTag.replace(/:/gi, "\\:")+(param ? param : ""); return( jd=="d" ? "#"+idTag : $("#"+idTag)); }
the param and jd parameters are optional (in fact jd is only necessary if you are using dojo)
Interesting Note
For the uninitiated you will note that jQuery returns a $() rather than a string – this is because it is actually returning a jQuery Object. That is a fundamental difference in the way that dojo and jQuery function as code libraries.
Examples
here is an example code of how it would be used in dojo or jQuery
//dojo dojo.query(x$("#{id:inputText1}", "", "d")).style("border", "1px solid red") //jQuery equivalent x$("#{id:inputText1}").css("background-image", "1px solid red")
An example is shown below
<?xml version="1.0" encoding="UTF-8"?> <xp:view xmlns:xp="http://www.ibm.com/xsp/core"> <xp:br></xp:br> <xp:scriptBlock id="scriptBlock1"> <xp:this.value><![CDATA[ function x$(idTag, param, jd){ //Updated 28 Feb 2012 idTag=idTag.replace(/:/gi, "\\:")+(param ? param : ""); return( jd=="d" ? "#"+idTag : $("#"+idTag)); } ]]> </xp:this.value> </xp:scriptBlock> <xp:inputText id="inputText1"></xp:inputText> <xp:br></xp:br> <xp:br></xp:br> <xp:button value="Change Border (Fail)" id="button2"> <xp:eventHandler event="onclick" submit="false"> <xp:this.script><![CDATA[dojo.query("#{id:inputText1}").style("border", "1px solid red")]]></xp:this.script> </xp:eventHandler> </xp:button> <xp:br></xp:br> <xp:br></xp:br> <xp:button value="Change Border X$" id="button1"> <xp:eventHandler event="onclick" submit="false"> <xp:this.script><![CDATA[dojo.query(x$("#{id:inputText1}", "", "d")).style("border", "1px solid red")]]></xp:this.script> </xp:eventHandler> </xp:button> </xp:view>