Zeeshan Rang
Zeeshan Rang

Reputation: 19885

Error: Id attribute is not allowed on the root tag of a component

I have a code like the one below

<mx:Button id="TestingID" width="100%" height="20">                   
    <mx:Script>
        <![CDATA[
             import flexlib.containers.WindowShade;
        ]]>
    </mx:Script-->
</mx:Button>

I am getting the error "id attribute is not allowed on the root tag of a component"

I have to give a Id to the button to refer to it. What should i do.. how do i solve this problem??

Regards Zeeshan

Upvotes: 2

Views: 2793

Answers (5)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

This can also happen if you are nesting a component inside another

As soon as you use the <fx:Component> tag you are in the root of an mxml-inline-document

<mx:itemEditor>
    <fx:Component>
            <mx:TextInput id="precioVenta"/>
    </fx:Component>
</mx:itemEditor> 

All you need to do is move the id attribute into the tag like this

<mx:itemEditor>
    <fx:Component id="precioVenta">
        <mx:TextInput />
    </fx:Component>
</mx:itemEditor> 

This applies to any kind of tag or nesting that causes the Flex compiler to create a new context for the inlinde declaration of a component

Upvotes: 1

Let see below code -

<s:ComboBox id="myCombo" dataProvider="{al}">
        <s:itemRenderer>
            <fx:Component>
                <s:CheckBox **id="NOWAY**" click="clickHandler(event)"/>
            </fx:Component>
        </s:itemRenderer>
</s:ComboBox>

id is not allowed in these sort of scenarios, you use of 'this' keyword, because in this context CheckBox is root tag within Component.

Upvotes: 0

Adrian
Adrian

Reputation: 155

if you are calling the component from within itself then you use the 'this' keyword.

<mx:Button height="20">                   
    <mx:Script>
        <![CDATA[
             import flexlib.containers.WindowShade;
             this.percentWidth = 100;
        ]]>
    </mx:Script-->
</mx:Button>

And if you want to refer to the custom component from your application then you do this.

<application xmlns:local = "[Directory containing custom component]">
    <local:MyCustomButton id="myButtonInstantiation" />
</application>

Make sense?

Upvotes: 3

James Ward
James Ward

Reputation: 29433

An MXML file is essentially a class. So if you want to reference the instance of that class from within it then you just use "this".

Upvotes: 1

rfunduk
rfunduk

Reputation: 30442

If you're defining this in a file as a subclass of Button then you can't set the id here. Put the id in the place you use this new component. For example, if this new component will be an AwesomeButton, you could use it like so:

<mycompnamespace:AwesomeButton id="testingId" />

Upvotes: 0

Related Questions