WannaBaCODER
WannaBaCODER

Reputation: 61

HTML in Sweet Alert

This has been answered several times but it seems Sweet Alert has made changes and html:true no longer works, just trying to add a clickable URL

Docs say HTML is no longer used. Instead, use the content object. but they don't really provide any examples

Below code works but displays the entire <a href .... </a> rather than just the CLICK HERE

swal({
  title: "TITLE HERE",
  //text: "<a href='#'>CLICK HERE<a>",
  html: true
});

Upvotes: 1

Views: 13463

Answers (3)

gtamborero
gtamborero

Reputation: 3212

@Niladri options is good for Sweet Alert 2.

      var form = document.createElement("div");
      form.innerHTML = `
      <span id="tfHours">0</span> hours<br>
      <input style="width:90%;" type="range" name="tfHours" value=0 step=1 min=0 max=25
      onchange="window.changeHours(this.value)"
      oninput="window.changeHours(this.value)"
      ><br>
      <span id="tfMinutes">0</span> min<br>
      <input style="width:60%;" type="range" name="tfMinutes" value=0 step=5 min=0 max=60
      onchange="window.changeMinutes(this.value)"
      oninput="window.changeMinutes(this.value)"
      >`;

      swal({
        title: 'Request time to XXX',
        text: 'Select time to send / request',
        content: form,
        buttons: {
          cancel: "Cancel",
          catch: {
            text: "Create",
            value: 5,
          },
        }
      }).then((value) => {
        //console.log(slider.value);
      });

I have created a Codepen with a Sweet Alert form data inside. Try it here!

Upvotes: 1

John Deptuch
John Deptuch

Reputation: 41

Sweet Alert can handle the creation of the html element for you. So the answer by Niladri can be rewritten as:

swal("Write something here:", {
    content: {
        element: "a",
        attributes: {
            href: "http://www.stackoverflow.com",
            innerText: "Click here"}}});

Upvotes: 4

Niladri
Niladri

Reputation: 5962

This code should work . You can use content now with specific DOM element

var el = document.createElement("a");
el.href = "www.stackoverflow.com";
el.innerText = "Click here";
swal("Write something here:", {
  content: el,
});

check this here : https://sweetalert.js.org/docs/#content

as shown in the sample you can pass a slider input to the alert

Here is a working fiddle https://jsfiddle.net/vq13hac4/2/

Upvotes: 4

Related Questions