DEV Community

Cover image for Alpine JS : An Intro
SumantaWeb
SumantaWeb

Posted on

4 2

Alpine JS : An Intro

Alpine JS : An Intro

Topics

  • Overview
  • How to Code
  • Creating a basic button

Overview

Your new, lightweight, JavaScript framework. -Alpine Js

It is a light weight js framework inspired by Vue JS!

How To Code

For it you have this script tag --
<script src="https://unpkg.com/alpinejs" defer></script>

but in the site it will be showing like this
<script src="//unpkg.com/alpinejs" defer></script>
just add https: before //unpkg.com

so for writing code just add the script tag inside the head tag

<head>
  <title>Title</title>
  <script src="https://unpkg.com/alpinejs" defer></script>
</head>
Enter fullscreen mode Exit fullscreen mode

Now in the body tag add a attribute like this
<body x-data="data()"></body>

Now add another script tag! And write the following inside it!

<body x-data="data()">
  <script>
  function data(){
    return{

    }
  }
  </script>
</body>
Enter fullscreen mode Exit fullscreen mode

Now understand what was data() it would fetch all the data from here.

So becoz it is an object so add variables like this varName : "value"

Creating a basic button

So lets create some variables!

<script>
  function data(){
    return{
      shown : false,
      click(){

      },
    }
  }
  </script>
Enter fullscreen mode Exit fullscreen mode

Here there is a var called shown with false as its value, and a function named click() so lets code them all.

<button>Toggle</button>
<div>Content</div>
Enter fullscreen mode Exit fullscreen mode

Add these.

Give a attribute x-show="shown" to the div. (x-show means if the value is being shown or not [it is an if statement], it goes away[the button] because the var shown is false if it is true the button will be visible).

Now give a attribute x-on:click="click()" to the button (it will run when the button is clicked).

Coding the function.

click(){
        this.shown = !this.shown
},
Enter fullscreen mode Exit fullscreen mode

Now it will toggle the shown var as true and then false!

Enjoy!

All the code :-

<head>
  <title>Title</title>
  <script src="https://unpkg.com/alpinejs" defer></script>
</head>
<body x-data="data()">
  <button x-on:click="click()">Toggle</button>
  <div x-show="shown">Content</div>
  <script>
  function data(){
    return{
      shown : false,
      click(){
        this.shown = !this.shown
      },
    }
  }
  </script>
</body>
Enter fullscreen mode Exit fullscreen mode

Consider Following??

Dev.To
GitHub

AI Agent image

How to Build an AI Agent with Semantic Kernel (and More!)

Join Developer Advocate Luce Carter for a hands-on tutorial on building an AI-powered dinner recommendation agent. Discover how to integrate Microsoft Semantic Kernel, MongoDB Atlas, C#, and OpenAI for ingredient checks and smart restaurant suggestions.

Watch the video 📺

Top comments (0)

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

đź‘‹ Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay