new
makes a new HTML element with a type, attributes and children
The new function is the heart of ClearlyJS: it allows you to create HTML elements with attributes and children!
Parameters
JavaScript parameters
type: type of the HTML element (for example h1, div, etc...) or your own element name (for example message, setting, etc...)
attributes: the element's HTML attributes (and the attributes specified in editAttributes)
...children: children of the element - each element in a new parameter
JSX parameters
In JSX the new function is different:
<type parameters> ...children </type>
Returns
the ClearlyHTML element
Examples
Creates a new red title with the text: "hello"
This syntax is a little bit messy, isn't it?
Creates a new div and puts a title inside the div
More with JSX
JSX allows you to understand more of your code! Here are some examples of what you can do with it:
variables
You can insert a variable to a JSX element (and its attributes) by using curly brackets:
Shows the value of x
events
events and listeners - such as onclick can be assigned to JSX elements in a second:
Assign an onclick event to a button that alerts "hello world"
return (ClearlyJS components)
JSX and ClearlyHTML elements can be returned from functions and make some super useful components:
Every time the function is called - a new div with the specified title and content is being made and assigned to the messages div!
Last updated