How to document.getElementById twice on same page?

Good morning peeps! I use this to hide my email. Its a web service that asuficates it. I modified it to use document.getElementById. How would I go about using this when I want to have the email address display more than once on the same page? As is it only works on the first id it hits. Obviously I could use the script twice with a different id. But is there a better way?? Thanks!


// Hide Email
var s="=b!isfg>#nbjmup;dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn#?dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn=0b?";m="";for(i=0; i<s.length; i++)m+=String.fromCharCode(s.charCodeAt(i)-1);document.getElementById('hideemail').innerHTML=(m);

A id’s are designed to be unique id’s, you can use a class name instead.


function hideEmail(el) {
    // hide emails in here, where el is the element containing a hidden email
    ...
}

var hiddenEmails = document.querySelectorAll('.hideemail'),
    i;
for (i = 0; i < hiddenEmails.length; i += 1) {
    hideEmail(hiddenEmails[i]);
}

So from the code you posted, instead of using document.getElementById(‘hideemail’).innerHTML, in the function you would instead use el.innerHTML

Cool thank you Paul! I will implement imeadiatly…

Ok I know this isn’t correct. But how do I replace the get element by id bit and make this correct? Thnaks!

<script type="text/JavaScript">
function hideEmail(el) {
    var s="=b!isfg>#nbjmup;dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn#?dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn=0b?";m="";for(i=0; i<s.length; i++)m+=String.fromCharCode(s.charCodeAt(i)-1);document.getElementById('hideemail')el.innerHTML=(m);
}
 
var hiddenEmails = document.querySelectorAll('hideemail'),
    i;
for (i = 0; i < hiddenEmails.length; i += 1) {
    hideEmail(hiddenEmails[i]);
}
</script>

I had a minor spelling mistake in my above code.

It’s a css selector that you use in the querySelector, so if it’s an id then you prefix the name with #, and if it’s a class then you prefix it with a fullstop.
To select all elements with a class of hideemail, you would use document.querySelectorAll(‘.hideemail’)

So what you should do is to replace the id=“hidemail” from your HTML with class=“hideemail”

Ok I can’t seem to get it working. Lets do this! Here is the full test page. Can you see my mistake?

<!DOCTYPE html><html><head><meta charset="UTF-8">
<title></title>
<style type="text/css">
/* --- Contact --- */
.hideemail {
word-break:break-word;
}
.hideemail b {
display:none;
}
</style>

</head>
<body>

<span class="hideemail">test.com</span>

<br>
<br>

<span class="hideemail">test.com</span>

<script type="text/JavaScript">
function hideEmail(el) {
    // Hide Email
var s="=b!isfg>#nbjmup;dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn#?dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn=0b?";m="";for(i=0; i<s.length; i++)m+=String.fromCharCode(s.charCodeAt(i)-1);document.querySelectorAll('.hideemail')el.innerHTML=(m);
}
 
var hiddenEmails = document.querySelectorAll('.hideemail'),
    i;
for (i = 0; i < hiddenEmails.length; i += 1) {
    hideEmail(hiddenEmails[i]);
}
</script>
</body>
</html>

Yes I can.

:smiley:

And to be more helpful, it resides in the hideEmails function.

Come on bro throw me a bone. Can you please tell me how to fix? I do not know.

Aw man, and I was about to post the solution :slight_smile:

To expand on the above, use your browser console to check the page for errors.
Also, if you use proper indentation then things will become more apparent.

Ok, in absence of any further posts, here you go:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Email obfuscation</title>
  </head>

  <body>
    <span class="hideemail">test.com</span><br><br>
    <span class="hideemail">test.com</span>

    <script type="text/JavaScript">
      function hideEmail(el) {
        var s="=b!isfg>#nbjmup;dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn#?dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn=0b?",
            m="";

        for(var i=0; i < s.length; i++){
          m += String.fromCharCode(s.charCodeAt(i)-1);
          el.innerHTML=(m);
        }
      }
       
      var mailAddresses = document.querySelectorAll('.hideemail');
      for (var i = 0; i < mailAddresses.length; i += 1) {
          hideEmail(mailAddresses[i]);
      }
    </script>
  </body>
</html>

HTH

Hmm - wouldn’t it be better to leave the innerHTML out of the for loop, and do it afterwards - or is this a rabbit hole the like of which we do not want to get in to?

I was leaving the whole “is this even a good idea” side of things alone.
I just refactored the code until it worked and then posted it after the conversation went quiet.

But you’re right, having it within the for loop is superfluous.
Well spotted :slight_smile:

Thank you very much bro. So how should I remove the inner HTML from the for loop?

Simples :slight_smile:

Take this line:

el.innerHTML=(m);

and place it after the for loop.

At the risk of sounding stupid - like this?


<!DOCTYPE html><html><head><meta charset="UTF-8">
<title></title>
<style type="text/css">
/* --- Contact --- */
.hideemail {
word-break:break-word;
}
.hideemail b {
display:none;
}
</style>
</head>
<body>
<span class="hideemail">test.com</span>
<br>
<br>
<span class="hideemail">test.com</span>
<script type="text/JavaScript">
      function hideEmail(el) {
        var s="=b!isfg>#nbjmup;dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn#?dpoubduAopsuitboejfhpqspqfsuznbobhfnfou/dpn=0b?",m="";
        for(var i=0; i < s.length; i++){
          m += String.fromCharCode(s.charCodeAt(i)-1);
        }
		el.innerHTML=(m);
      }
      var mailAddresses = document.querySelectorAll('.hideemail');
      for (var i = 0; i < mailAddresses.length; i += 1) {
          hideEmail(mailAddresses[i]); 
	  }
    </script>
</body>
</html>

And above see you said this may not even be a good idea? Are you referring to the code or the method of email absofication? Because I find it to work perfect as a spam deterrent. Thanks!

Hi Eric,

I meant more that it has accessibility issues and it’s effectiveness has been called into question.
Obfuscate no more: why your email address should go au naturale

Thnaks Pullo :slight_smile: