0

I trying to get the #email div content insdie my javascript code.

Example:

<div id="email">useremail@gmail.com</div>

I want to print email address here

document.write('<img src="' + get_gravatar('Email Here', 150) + '" />');

Thank you very much!

Ali Najm
  • 13
  • 4

5 Answers5

1

I would suggest to not use document.write.

If I understand you, using jquery:

(function($) //IIEF format
{
    $(function() //wait for doc ready
    {
        var $img = $('<img></img>'); //create img tag using jquery
        var email = $('#email').text().trim() //could also be .html() to get inner html
        $img.attr('src', get_gravatar(email, 150) ) //add email to src attribute of img tag 
        $('body').append($img); //change 'body' with whatever you want to add it to; this will append the img tag into the document
    }
})(jQuery);
Community
  • 1
  • 1
James Haug
  • 1,286
  • 10
  • 25
  • Not working :/ , try it please [Here](http://www.w3schools.com/jquery/tryit.asp?filename=F0O577DN2ZOO) – Ali Najm Nov 11 '16 at 05:44
  • When trying to run it, I'm getting an error from inside the get_gravatar function: http://www.w3schools.com/code/tryit.asp?filename=F0O5N1T083IP – James Haug Nov 11 '16 at 05:58
  • Omg, it's working fine! , thank you very very much my friend :) – Ali Najm Nov 11 '16 at 06:05
  • Cheers. Great to hear. Fixed my answer to wait for the doc ready (which was the only real change I did in the tryit page). – James Haug Nov 11 '16 at 06:10
0

First you will need to get the text and store it in a variable and then you can use that.

You can use the following:

var email = document.getElementById('email');
document.write('<img src="' + get_gravatar(email, 150) + '" />');
Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92
0

Use .innerHTML or .textContent in javascript of the div email

The innerHTML property sets or returns the HTML content (inner HTML) of an element.

var emailId = document.getElementById('email').innerHTML;
document.write('<img src="' + get_gravatar(emailId, 150) + '" />');

or

textContent returns null if the element is a document, a document type, or a notation. To grab all of the text and CDATA data for the whole document, one could use document.documentElement.textContent.

var emailId = document.getElementById('email').textContent;
document.write('<img src="' + get_gravatar(emailId, 150) + '" />');
Sudharsan S
  • 14,830
  • 3
  • 27
  • 47
0
document.write('<img src="' + get_gravatar(document.getElementById("email").innerHTML, 150) + '" />');

JQuery

document.write('<img src="' + get_gravatar($("#email").val(), 150) + '" />');
0

Hope this helps You,

var mail = document.getElementById('email').innerHTML;
document.write('<img src="' + get_gravatar(mail, 150) + '" />');
siva
  • 224
  • 2
  • 11