Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a JSF Maven project in which i need to change some html properties dynamically like rendered etc through javascript. I have so far tried following functional examples which are working for style and readOnly clauses but for rendered, javascript function show "undefined"

What I have tried:

<pre>
function visibleInVisible(ctrl, visible) {
    try {
        var c = document.getElementById(ctrl).rendered;
        alert(ctrl + ", " + c);
        c = document.getElementById(ctrl).style;
        alert(ctrl + ", " + c);
//        document.getElementById(ctrl).style.display = 'none';
//        document.getElementById(ctrl).readOnly = true;
//        document.getElementById(ctrl).render = false;
    }
    catch (err) {
        alert('Error ! ' + err.message);
    }
}


would any body help me suggesting a way to accomplish the task.
Posted
Updated 30-Jul-21 0:38am

1 solution

rendered isn't a valid attribute for a HTML element, check the Element - Web APIs | MDN[^] documentation page.

If this is a custom attribute then you should probably prefix it with data- to indicate that it shouldn't be considered an actual DOM attribute, but a transient one. I also found this StackOverflow link[^] which indicates some attributes may be server-side and stripped before they reach the browser, in which case you wouldn't be able to access the attribute at all.

If you want to use the data- approach then you can add an attribute called data-rendered="value" and then access it using the getAttribute() method:
JavaScript
const element = document.getElementById('..');

// check if the attribute exists
if (element.hasAttribute('data-rendered'))
{
  // get the attribute
  const rendered = element.getAttribute('data-rendered');
  ..
  // set the attribute
  element.setAttribute('data-rendered', 'value');
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900