ForgejComponent
en

jComponent

Show, hide and style elements with ui-bind

jComponent 20 · jcomponent@20 jcomponentfrontendbinding Executed with jComponent 20

<ui-bind path="..." config="..."> applies commands to its element whenever the value of the path changes. Commands are separated by semicolons. Each command is name:expression, where value is the current value of the path:

  • show:value / hide:value shows or hides the element
  • html:value / text:value replaces the content
  • .classname:value adds the class when the expression is truthy
public/index.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<title>jComponent</title>
	<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
	<script src="//cdn.componentator.com/spa.min@20.js"></script>
	<style>.hidden { display: none; } .admin { color: crimson; }</style>
</head>
<body>

	<ui-bind path="user.admin" config="show:value" class="hidden">Admin panel</ui-bind>
	<ui-bind path="user.admin" config="hide:value">You are a guest</ui-bind>
	<ui-bind path="user.name" config=".admin:value === 'Peter';text:value">-</ui-bind>

	<script>
		SET('user.admin', false);
		SET('user.name', 'Louis');
	</script>

</body>
</html>
Try it in your browser

Edit the example and run it. It runs in an isolated frame in your browser; nothing is sent to our server.

Preview