Input text field that works with a mailto: a href

I want to add a phone number to part of a mailto a href.

I would like a text input field for a number, once the number is entered you click on a link:

<a href="mailto:number@vtext.com?subject=From: ....etc

The number in the input field replaces the number before the @ sign.

So once you click on the link, an email will pop-up in your email client and be this:

5555551234@vtext.com in the TO field.

Now you can text from an email to this number.

I know how to do this by creating a form that posts to a another page and I can do this:

mailto:<%=number%>@vtext.com?

That is ASP Classic and I can easily do that. That would insert the number into that mailto link.

But I am lost here because I would like to do this without posting and do this all on the same page.

I am posting this to JS rather than html/css because I don’t see a way to simply do this. I hope I am wrong and I am just having a brain fog.

Thank you

Take a look at the code below…

First the link. Notice we just attached a class to it so we can select any links with this associated class.

<a href="mailto:number@vtext.com" class="mailto">Test link</a>

Next we create a simple little event listener which we attach to these links. They will read the href attribute of the link, do a replace, then redirect the window location to the new mailto. This should do what you are looking for.

// After window loads, select all the links with the 'mailto' class.
window.onload = () => {
	let mailtos = document.querySelector('.mailto');
        
	// Attach an event listener listening for clicks
	mailtos.addEventListener('click', (e) => {
       
		// Prevent the link from triggering its normal behavior
  		e.preventDefault();

		// Read the current mailto href
		let href = e.target.getAttribute('href');

		// Do a simple replace and set the window location to it. This should trigger the default application
		// The user has associated with the mailto protocol. It will launch with mailto:555@vtext.com
		window.location.href = href.replace('number', '555');
  });
};

Hopefully you see what is being done here and adjust it as necessary.

Thank you for that, I appreciate it. Not quite there yet, close though.

What I need is an input box (like a form text input) where the number can be typed in, rather than hard code the 555. So rather than the 555@ it will be the number@ (from the input box).

Do you think that’s possible to do?

Thank you

One other thing:

I have a list of about 10 cell carriers with the same concept. When I put the class on the others, the 555@ doesn’t work. Does the class=“mailto” code only work with the first one, or just one a href?

I wanted one text box and regardless of the email linked, the number would insert. Or I could have a separate input box next to each of the cell carrier numbers.

Ideally, one input box would be best.

The solution I showed was an example… I put a hard coded ‘555’ in there to show you where to put your value. So the only thing you have to do is instead read from the input and put its value there. Why not try to do this yourself? Do a search for how to read values from an input box.

As for multiple links… you can select them all and then use a forEach to loop through them, attaching an event listener to them.

// After window loads, select all the links with the 'mailto' class.
window.onload = () => {
	let mailtos = document.querySelectorAll('.mailto');

	// Attach an event listener listening for clicks
	mailtos.forEach(element => element.addEventListener('click', (e) => {
       
		// Prevent the link from triggering its normal behavior
  		e.preventDefault();

		// Read the current mailto href
		let href = e.target.getAttribute('href');

		// Do a simple replace and set the window location to it. This should trigger the default application
		// The user has associated with the mailto protocol. It will launch with mailto:555@vtext.com
		window.location.href = href.replace('number', '555');
  }));
};
1 Like

This is where I get lost. Text input is pretty simple for ASP and getting values to. JS is different and my research is not getting me anywhere.

It looks like I can replace the ‘555’ with a value in the input box. In my reading, for JS I think I have to have an ID.

 <form class="mailto">
        <input type="text" id="mynumber">
    </form>

Then…

```
window.location.href = href.replace('number', 'mynumber');
```

But that does not work and I can’t seem to find an answer that parallels this example.

I do looping with sql statements for databases but don’t understand how in JS.

Here you are replacing the string ‘number’ with the string ‘mynumber’. The second one of those should likely be a variable.

In the original example we had this:

window.location.href = href.replace('number', '555');

By clicking on the mailto link, the 555 replaces the word ‘number’. So it looks like to me that I need a variable that goes in the 555 slot.

Did I use the form input correctly?

Right :slight_smile:

You need to grab the value of the desired input using JavaScript, for example:

const myInput = document.querySelector('what-goes-here-depends-on-the-structure-of-your-markup');
window.location.href = href.replace('number', myInput.value);
1 Like

This is where I get lost. I see that you replaced ‘number’ with myInput.value.

What I don’t understand is does myInput go into the id of the form? I tried that with no results. Because apparently there is more to it than that because you add a const myInput line and I don’t know where that belongs because I though all I needed was to add an input box to the original input form.

I suggest you read up on what querySelector does and its value attribute.

You seem to not know what this does and how it might be getting a value out of your form.

Oh, I have read up on it but don’t understand it. I am not a professional coder and I am pretty good with ASP and SQL. But I don’t get JS, sorry! I know pro coders that are not good at it either, that tells me it isn’t all that easy. I look at this forum to get coding or examples, like other websites that have forums, to place on my websites. But I do thank you for your attempt and I wish I had your knowledge to help someone else. There is a definitely a few really smart coders on this forum, certainly you are one of them and of course @James_Hibbard is another brilliant guy. Wish I had a fraction of what you both know. But I appreciate your time, thank you.

Howdy,

Can you please post an example of the markup (i.e. HTML) you are working with.

Note: I don’t mean the ASP code used to generate the HTML, rather what is rendered to the page.

This is the form that I put together, but not sure if correct. (My ASP form code is different)

<form class="mailto">
<center><input type="text" placeholder="Your number here" id="myInput"></center>
</form>

I took out all of the asp code that puts users name in the email and subject line and onclick tracking code, etc.

So I simplified this with breaks to see that I have 4 cell carriers for this example.

Eventually, more will be added that will be inserted into the loop, once I understand how that works in JS.

I added the class to each mailto link. Perhaps that’s not the best way to do that, but I think it works. Certainly willing to learn a better way to do that.

<p align=center>
AT&T – <a href="mailto:number@txt.att.net class="mailto";">click here</a>
<br />
Sprint –  <a href="mailto:number@messaging.sprintpcs.com class="mailto";">click here</a>
<br />
T-Mobile –  <a href="mailto:number@tmomail.net class="mailto";">click here</a>
<br />
Verizon – <a href="mailto:number@vtext.com class="mailto";">click here</a>
</p>

This is the JS I currently have:

<script>
    // After window loads, select all the links with the 'mailto' class.
window.onload = () => {
	let mailtos = document.querySelector('.mailto');
        
	// Attach an event listener listening for clicks
	mailtos.addEventListener('click', (e) => {
       
		// Prevent the link from triggering its normal behavior
  		e.preventDefault();

		// Read the current mailto href
		let href = e.target.getAttribute('href');

		// Do a simple replace and set the window location to it. This should trigger the default application
		// The user has associated with the mailto protocol. It will launch with mailto:555@vtext.com
		window.location.href = href.replace('number', 'myInput.value');
  });
};
</script>

Thank you James, I really appreciated your time.

Hi,

You can do it like this:

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="utf-8">
  <title>Auto-populate carrier list</title>
  <style>
    .carriers {
      display: none;
    }
  </style>
</head>
<body>
  <form>
    <input type="text" placeholder="Your number here" id="userNumber">

    <div class="carriers">
      AT&T – <a href="#" data-carrier="txt.att.net" class="mailto">click here</a>
      <br />
      Sprint – <a href="#" data-carrier="messaging.sprintpcs.com" class="mailto">click here</a>
      <br />
      T-Mobile – <a href="#" data-carrier="tmomail.net" class="mailto">click here</a>
      <br />
      Verizon – <a href="#" data-carrier="vtext.com" class="mailto">click here</a>
    </div>
  </form>

  <script>
    const number = document.querySelector('#userNumber');
    const carriers = document.querySelector('.carriers');
    const mailtos = document.querySelectorAll('.mailto');

    number.addEventListener('keyup', function() {
      if (this.value === '') {
        carriers.style.display = 'none';
      } else {
        carriers.style.display = 'block';
      }

      mailtos.forEach((el) => {
        el.href = `mailto:${this.value}@${el.dataset.carrier}`;
      });
    });
  </script>
</body>
</html>

Note that when the input is empty, the carrier list with the “click here” links is not displayed. As soon as you type something in the box, then each of the links is displayed and the href attribute is populated with the correct value.

As @Martyr2 says, it would be good for you to understand what is going on here, so please ask if there is anything you don’t understand.

1 Like

James,

That’s very good!! There is noway I could come up with that! Even though I don’t understand the JS, I do understand what you did, to some degree!

So a couple of questions or comments:

  1. I see where the looping takes place with the forEach.

  2. I am guessing there are multiple ways of doing this if you understand JS? Your way seems different than @Martyr2, which I am sure if I had a better understanding, it would of worked.

  3. I get what you did because I incorporated it with my ASP and it worked.

  4. I like the fact that the carrier names don’t show until you start typing. Is the reason because the event doesn’t fire until you starting typing?

In summary, this is probably even more than I was looking for - better that is! Thanks for this and please know that I really appreciate your knowledge and the time you took to do it. It really helps.

1 Like

No problem :slight_smile:

Absolutely. That is what makes programming so accessible, but what also gives you ample opportunity to shoot yourself in the foot.

Initially, we are hiding the carriers with the CSS declaration at the top of the document.

number.addEventListener('keyup', function() { ... });

This adds some code that is executed every time the user presses a key in the input field.

if (this.value === '') {

Here this refers to the input element. If it is empty, then:

carriers.style.display = 'none';

we hide the carriers container. Otherwise:

carriers.style.display = 'block';

we display it.

Glad to hear that it works like you wanted.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.