Jithin Varghese
Jithin Varghese

Reputation: 2228

appending contents to a div using Javascript not working

I have a Javascript code for decryption, now i want to display the decryption string inside a div. I have tried using the following code.

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>

<script>
    var decrypted = CryptoJS.AES.decrypt("dsfsdffd", "dsfsdf");
    var dec = decrypted.toString(CryptoJS.enc.Utf8);

    document.getElementById("display1").innerHTML = dec;
</script>

<div id="display1"></div>

But the above code not working. What i have done wrong. Is there any solution.

Upvotes: 1

Views: 45

Answers (2)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

Since you have tagged the question with jQuery, here is the jQuery solution

 $(document).ready(function () {
   var decrypted = CryptoJS.AES.decrypt("dsfsdffd", "dsfsdf");
   var dec = decrypted.toString(CryptoJS.enc.Utf8);
   $("#display1").html(dec);
 });

Here you can place your script before the elements.

Upvotes: 1

Nikolay Ermakov
Nikolay Ermakov

Reputation: 5061

That's because your script is executed before the div is added to DOM. Move your script below the div, best before </body> tag.

Upvotes: 2

Related Questions