🛡️
Hogosuru
  • What is Hogosuru?
    • How it works?
    • Minimal requirements to load a wasm
    • Repositories
  • Guides
    • Configure your environment
    • Web API support
    • Create your first project
    • Load and parse a JSON data
    • Create HTML entities and manipulating DOM
      • Create some dynamic content
      • Attach to existing content
      • Modify CSS and attributes
      • Repeat content using template
    • Async work with Promise
    • Network IO: An example with gofiber
    • Observers: Your data always up to date
    • Create a single app with hogosuru
      • Example
Powered by GitBook
On this page
  • Example with Bulma.io
  • First Step: import the css
  • Let's take a closer look at the code
  • How to hide a component?
  • Access to attributes
  1. Guides
  2. Create HTML entities and manipulating DOM

Modify CSS and attributes

PreviousAttach to existing contentNextRepeat content using template

Last updated 3 years ago

You can modify attributes and elements, thanks to web api manipulation function.

I will not cover all the possibilities but give you some simple examples.

Example with Bulma.io

Bulma is an amazing CSS framework focused only on CSS.

I will show you how you can use easly with hogosuru

First Step: import the css

There are a lots of way to do this. Usually, you import it in the same time you load the wasm.

I will use another method and show you how you can load it dynamically

func main() {
	hogosuru.Init()

	// we get the current document
	if doc, err := document.New(); hogosuru.AssertErr(err) {

		//we got the head
		if head, err := doc.Head(); hogosuru.AssertErr(err) {

			if link, err := htmllinkelement.New(doc); hogosuru.AssertErr(err) {

				link.SetRel("stylesheet")
				link.SetHref("https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css")
				head.AppendChild(link.Node)

			}

		}

		if body, err := doc.Body(); hogosuru.AssertErr(err) {
			//lets create some button design with bulma

			if div, err := htmldivelement.New(doc); hogosuru.AssertErr(err) {

				if list, err := div.ClassList(); hogosuru.AssertErr(err) {
					list.Add("buttons")

				}

				if buttonprimary, err := htmlbuttonelement.New(doc); hogosuru.AssertErr(err) {

					buttonprimary.SetTextContent("Primary")
					//we get the class list attribute
					if list, err := buttonprimary.ClassList(); hogosuru.AssertErr(err) {
						list.Add("button")
						list.Add("is-primary")
					}

					buttonprimary.OnClick(func(e event.Event) {

						if list, err := buttonprimary.ClassList(); hogosuru.AssertErr(err) {

							list.Remove("is-primary")
							list.Add("is-warning")
						}
					})

					div.Append(buttonprimary.Element)
				}

				if buttondanger, err := htmlbuttonelement.New(doc); hogosuru.AssertErr(err) {

					buttondanger.SetTextContent("Danger")
					//we get the class list attribute
					if list, err := buttondanger.ClassList(); hogosuru.AssertErr(err) {
						list.Add("button")
						list.Add("is-danger")
					}

					buttondanger.OnClick(func(e event.Event) {

						if list, err := buttondanger.ClassList(); hogosuru.AssertErr(err) {

							list.Remove("is-danger")
							list.Add("is-info")
						}
					})
					div.Append(buttondanger.Element)
				}

				body.Append(div.Element)

			}

		}

	}

	ch := make(chan struct{})
	<-ch

}

Let's take a closer look at the code

First we get the head of the document, we create an htmllink with the ref to the bulma css and attach to the head

		if head, err := doc.Head(); hogosuru.AssertErr(err) {

			if link, err := htmllinkelement.New(doc); hogosuru.AssertErr(err) {

				link.SetRel("stylesheet")
				link.SetHref("https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css")
				head.AppendChild(link.Node)

			}

		}

Follow to the bulma documentation , we create an html code like this:

<div class="buttons">
  <button class="button is-primary">Primary</button>
  <button class="button is-danger">Danger</button>
</div>

We dynamically create the components, assign the class like the html above. When click on the Primary button, we change their class for warning. When click on the Danger button, we change their class for info.

if body, err := doc.Body(); hogosuru.AssertErr(err) {
	//lets create some button design with bulma

	if div, err := htmldivelement.New(doc); hogosuru.AssertErr(err) {

		if list, err := div.ClassList(); hogosuru.AssertErr(err) {
			list.Add("buttons")

		}

		if buttonprimary, err := htmlbuttonelement.New(doc); hogosuru.AssertErr(err) {

			buttonprimary.SetTextContent("Primary")
			//we get the class list attribute
			if list, err := buttonprimary.ClassList(); hogosuru.AssertErr(err) {
				list.Add("button")
				list.Add("is-primary")
			}

			buttonprimary.OnClick(func(e event.Event) {

				if list, err := buttonprimary.ClassList(); hogosuru.AssertErr(err) {

					list.Remove("is-primary")
					list.Add("is-warning")
				}
			})

			div.Append(buttonprimary.Element)
		}

		if buttondanger, err := htmlbuttonelement.New(doc); hogosuru.AssertErr(err) {

			buttondanger.SetTextContent("Danger")
			//we get the class list attribute
			if list, err := buttondanger.ClassList(); hogosuru.AssertErr(err) {
				list.Add("button")
				list.Add("is-danger")
			}

			buttondanger.OnClick(func(e event.Event) {

				if list, err := buttondanger.ClassList(); hogosuru.AssertErr(err) {

					list.Remove("is-danger")
					list.Add("is-info")
				}
			})
			div.Append(buttondanger.Element)
		}

		body.Append(div.Element)

	}

}

Click on 2 buttons

How to hide a component?

We can hide components with the style directive "Display:None".

To hide a button:

if css,err:=buttondanger.Style();hogosuru.AssertErr(err) {
css.SetProperty("display", "none")
}

Access to attributes

If you want create an input checkbox element, you need write

if input, err := htmlinputelement.New(d);hogosuru.AssertErr(err) {
input.SetAttribute("type", "checkbox")
}

Style can be changed with the SetProperties and RemoveProperties of the

You can Modify/Set the attributes of elements with the corresponding method available on

👍
CSSStyleDeclaration
Element