Search code examples
liferayliferay-6alloy-uiliferay-aui

Alloy user interface (access a tag value)


I'm working with liferay portal 6.2. And I want to get the value of the text in a tag with alloy user interface. exemple:

<div> 
    <p> Paragraph </p>
    "value"
</div>

the desired result is: value

please help.


Solution

  • AlloyUI, being an extension of YUI3, uses get/set methods to access and manipulate the properties/attributes of the object (YUI3 Node / AlloyUI Node) that is returned when looking up elements from the page.

    Some examples can be reviewed in this documentation as well as this documentation.

    In general you'll need something unique (i.e. id, css class) to the div in order to fetch only that element. Once you have that element, divNode.get('text') will give you all of the text within the element. There is not a means to easily "skip" the paragraph contents within the div without the value being contained within some other markup. If you have control over the markup and can do this, that would be the best option. Otherwise you are left to using the replace function to strip out the paragraph contents from the text.

    <script>
      AUI().use('aui-base', function(A) {
    
       var paragraphText = A.one('#myDiv>p').get('text');
       var divText = A.one('#myDiv').get('text')
    
       var onlyValue = divText.replace(paragraphText, "").trim()
       console.log(onlyValue)
    
      })
    </script>