daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 276 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Getting an anchor value which is inside a li tag
  2. <li class="email">
  3.     <a href="mailto:abc@gmail.com">abc@gmail.com</a>
  4. </li>
  5.    
  6. var elem = document.querySelectorAll('.email');
  7. var email = elem[0].children[0].innerHTML;
  8.    
  9. var elems = document.querySelectorAll('.email');
  10. for(var i = 0; i < elems.length; i++) {
  11.     var elem = elems[i];
  12.     console.log(elem.firstElementChild.getAttribute('href').substr(7));
  13. }
  14.    
  15. [].forEach.call( document.getElementsByClassName( 'email' ), function( el ) {
  16.     console.log( el.firstElementChild.textContent );
  17. } );
  18.    
  19. function getElsByClassName(classname){
  20.     var rv = [];
  21.     var elems  = document.getElementsByTagName('*')
  22.     if (elems.length){
  23.         for (var x in elems ){
  24.             if (elems[x] && elems[x].className && elems[x].className == classname){
  25.                 rv.push(elems[x]);
  26.             }
  27.         }
  28.     }
  29.     return rv;
  30. }
  31.  
  32. //Get the li
  33. var li = getElsByClassName("email"); //Alass, it returns an array
  34. var a = li[0].firstChild;
  35. var email = a.getAttribute('href').substr(7);
  36.    
  37. var email=document.getElementsByClassName('email')[0].getElementsByTagName('a')[0];
  38.    
  39. alert(email.innerHTML); // will print abc@gmail.com
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top