stratoflow

Differences in using CSS, JQuery and Java Script with ID in Visualforce Page controllers

Today we’re gonna focus on what’s different between CSS, JQuery and JS when it comes to Visualforce page controllers. It’s quite important to know that since a mistake can lead into some problems.

As you probably are aware, using Java Script and JQuery is a little different when it comes to Visualforce Page and HTML tags. The main reason for this is that Visualforce code is converted into HTML code in user end page. In a nutshell, HTML code is generated from Visualforce code. When we are using Visualforce tags instead of HTML tags our id’s names are altered in the end HTML page.

In the following example we are able to see how ID of apex:outputText was changed:

<apex:outputText styleClass="abc" id='t'> abc </apex:outputText> 

We are leaved in end user page with this code :

abc
<span id="j_id0:theform:t" class="abc"></span>

In this case how can we add CSS look or JS code to Visualforce page? We can of course use class name to do this because as we can see that there wasn’t any changes made to its name. But we cannot forget that class name isn’t a unique name for tag and it can be used in different places therefore it isn’t the best practice. Better solution to that problem is still using  Id attribute but we must refer to it other way. Another interesting aspect in this example, is that even if we add text to Visualforce tags we can’t be sure whether it will be shown inside the tags the way we intend it or not.

To feed Java Scrpit to Visualforce it is best to use this: {!$Component.IdName} formula to take proper Id name from Visualforce Page:

<apex:outputPanel styleClass="abc" id='t'> abc </apex:outputPanel>

var y= document.getElementById('{!$Component.t}');

In this particular example, if we use apex:output panel then we will get text inside HTML tags. It is also worth keeping in mind that stuff like this does like to happen.

the other case, while using Jquery we will also need to fill Id in other way than in normal HTML code. Best way of doing this is probably in the following example. You can notice that id name for visualforce tags in JQuery is inserted according to this formula: [id$=IdName].

<apex:inputText id="output" html-placeholder="Click here"/>

$('[id$=output]').click(function(){
alert('InputText clicked');
}

And now let’s move on to feeding Visualforce tag with custom CSS layout using Id. Best way of working this out is doing this like it is presented in the example below, and just to clarify: if you want to connect CSS file tag you’d need to write ID with this particular formula: [id*=IdName].

<apex:outputText id="myId" value="This is less fancy."/>

<style type="text/css">
[id*=myId] { font-weight: bold; }
</style>

All of this is showing us that you need to be very careful with Visualforce and always make sure that code is behaving just like you intend. Hope these tips will turn out to be helpful!

Testimonials

The developed software product was built from scratch with solid quality. We have had a long-term engagement with Stratoflow for nearly 10 years. We look at them as partners, rather than contractors. I'm impressed by their team culture and cross-team support.

Nathan Pesin

CTO, Legerity Financials

Stratoflow was a great partner, challenging as well as supporting our customer projects for the best outcome. They have a great pool of talent within the business - all very capability technologists, as well as being business-savvy and suitable for consultancy engagements.

Chris Goodall

Managing Consultant, CG Consultancy (UK) Limited

The bespoke metal exchange platform works great, it is easily accessible and richly functional. Stratoflow managed deadlines capably, meticulously documented their progress, and delivered a complex project at an affordable cost.

Bartlomiej Knichnicki

Vice Chairman, Supervisory Board

We are very pleased with our partnership with Stratoflow and, as we continue to grow, we expect to increase the numbers of developers that work with us on our projects. They have proven to be very skilled and flexible. They're extremely reliable, and they have a very good company culture of their own, which gives them a real edge compared to other providers that serve more as production shops rather than thought partners and creative problem solvers.

Andrew Kennedy

Founder & Managing Director, Tier 2 Consulting

Stratoflow successfully customized the system according to the specific functionalities and without bugs reported. The team was commended for their adaptability in the work process and for their responsiveness.

Joshua Blavins

Tech PM, Digital Agency

The features implemented have received overwhelmingly positive feedback from end-users. Stratoflow has an incredible technical expertise and a high degree of flexibility when it comes to changing project requirements.

Adam Hill

Chief Technology Officer, Legerity

They have impressively good knowledge of AI issues. Very responsive to any amendments and findings. Very good communication. We received a finished project which could be implemented into production shortly after testing.

CO-Founder & CTO

Circular Fashion Company

They provided superb service with seamless communication and a highly professional, technical approach. The team displays impressive technical expertise and are willing to share information and engage in constructive feedback.

Filip Stachnik

Operations Manager, Otwarte Klatki (part of Anima International)

They're very skilled technically and are also able to see the bigger picture. Stratoflow can actually think about solutions, not just the technical task at hand, which they've been assigned.

Arnd Jan Prause

Chief Operating Officer, musQueteer

Stratoflow delivered the website successfully within the timeframe and budget. They assured that the output met the set requirements. Overall, the team's performance was excellent and recommended for their exceptional technical business expertise. They've been able to deliver all of their work on time and within budget, which has been very impressive.

Lars Andersen

Founder & CEO, My Nametags

Travel sector rebound after the pandemic is complete. We have fantastic global coverage of travel data distribution due to mutual agreements and data exchange between aggregators. Competition for the best price of limited resources degradates margins.

How to win? Provide personalized experience and build your own products in the front-office. The missing bits: a traveller golden record collecting past activities and a AI/ML recommendation technology.

Michał Głomba

CEO at Stratoflow