简体   繁体   中英

How to change the contents of dynamically created div tags?

My javascript function is as below. It gets all the parameter values. But the function is not working. Please any suggestions?

function getProductData(dealNo,title,url)
{
    // following line is not working
    document.getElementById(dealNo).innerHTML = "whatever";

}

edit: dealNo is the id of the dynamically created div tag

 document.getElementById('dealNo').innerHTML = "whatever";

Your code should be OK. This is example from your code http://jsfiddle.net/4VNL7/ . Check is dealNo getting correct id and that id is already in HTML. Maybe you are trying to modify div that is not in HTML.

if you just want to add text to an existing div, try this document.getElementById('dealNo').innerText = "Hello"

of if you want to place an html to the existing div, try this document.getElementById('dealNo').innerHTML = "Hello"

if they are not working, see whether your div Id is indeed 'dealNo'.

You can also use jQuery to add to text div which are dynamically added.

HTML

<div id="containerDiv"></div>

jQuery

var divVariable = $('div');
$(divVariable).html('This is text in div');
$('#containerDiv').append(divVariable);

In above code I have created divVariable with div HTML instance, also appended text to that new created div in memory. Finally that div is appended to containerDiv (ie parent div).

Dynamically div is child div.

Check this Fiddle Demo

Sorry,It was my fault. I haven't cleared the magento cache. That was the issue. Thank you all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM