DEV Community


Click to send on WhatsApp with JavaScript

mandaputtra profile image Manda Putra Updated on ・1 min read

TL;DR You can use link below and place it on your button, or whatever. Its simply will open up whatsapp on user phone. But it isn't automatically send.*YOURNUMBER*&text=%20*YOURMESSAGE*

What we were built here it is a dynamic send to WhatsApp link with JavaScript approach you can use the link above and give it a try

Okay I'll use JSBin with this, first open up then its just easy you will use split() and join() string property on JavaScript here my script.

//{{ *YOURNUMBER* }}&text=%20{{ *YOUR MESSAGE* }}

var yourNumber = "{{ your number in string}}"
var yourMessage = "{{ your message in string }}"

// %20 mean space in link
// If you already had an array then you just join them with '%20'
// easy right

function getLinkWhastapp(number, message) {
  number = yourNumber
  message = yourMessage.split(' ').join('%20')

  return console.log('' + number + '&text=%20' + message)


Copy that on jsbin, give phone number and message do you want, make sure you use WhatsApp, It will open up WhatsApp on your phone with the message you wrote.

Further reading :


This post was made to learn JS split and join for production purpose you should use encodeURIComponent()

function getLinkWhastapp(number, message) {
  var url = '' 
     + number 
     + '&text=' 
     + encodeURIComponent(message)

  return url


Editor guide
gustavosfq profile image
Gustavo Flores

maybe use encodeURIComponent() to encode this.message to URI

mandaputtra profile image
Manda Putra Author

Yeah thats nice, easy, error prone

gypsicoder profile image
Bishnu Pada Chanda

Is there any way to send image with text?

mandaputtra profile image
Manda Putra Author

No man its only accept url