Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I want to create some code on button click using javascript and jQuery,
those code look:

HTML
<div class="news">
    <img src="./images/1.jpg" />
    <content>
        <a class="person" href="">Lubomyr Pelyo</a>
        <br />
        <comment>
            First record in news
        </comment>
        <br/>
        <settings>
            <a href="">Comment</a>
            <a href="">Share</a>
            <a href="">Remove</a>
        </settings>
    </content>

    <br style="clear:both"/>
</div>


to create thet code I use:
JavaScript
$('#newPost').click(function(){
    var text = $('#new').val();
    var textTrim = $.trim(text);
    if(textTrim != ''){
        var newsDiv = document.createElement('div');
        newsDiv.setAttribute('class', 'news');
        var person = document.createElement('img');

        newsDiv.innerText = text;
        $('#news').prepend(newsDiv);
    }
    $('#new').val('');
});


I cant find how to create "div .news" with all those blocks inside.
Posted

So you aren't sure how to make a bunch of nested DOM elements? There are many ways:
  • jQuery. You are using jQuery, which can readily create DOM elements using HTML strings, like this:
    JavaScript
    var newsDiv = $("<div class\"news\"><img src=\"./images/1.jpg\" />...and so on...<div>")[0];

  • Copy. You can create the div in the HTML and set the style to "display: none", then fetch that element (e.g., using a jQuery selector), then copy the element using cloneNode (and you'll have to remove the display of none).
  • Manual. Create the child elements using createElement (just like you did for the news DIV), then add them to the parent using appendChild
 
Share this answer
 
Button and container div markup is given by
HTML
<input type="button" id="injectDynamicMarkup" value="Click">
<div id="DynamicDiv"></div></input>


Script add dynamic markup is as follows

JavaScript
var dynamicMarkup='<div class="news">'+
                '<img src="./images/1.jpg" />'+
                '<content>'+
                    '<a class="person" href="">Lubomyr Pelyo</a>'+
                    '<br />'+
                    '<comment>'+
                        'First record in news'+
                    '</comment>'+
                    '<br />'+
                    '<settings>'+
                        '<a href="">Comment</a>'+
                        '<a href="">Share</a>'+
                        '<a href="">Remove</a>'+
                    '</settings>'+
                '</content>'+
 
                '<br style="clear:both" />'+
            '</div>';
$("#injectDynamicMarkup").click(function(){
    $("#DynamicDiv").html(dynamicMarkup);
});
 
Share this answer
 
if someone interest
JavaScript
if(textTrim != ''){
      var newsDiv = document.createElement('div');
      newsDiv.setAttribute('class', 'news');
      var img = document.createElement('img');
      img.setAttribute('src', './images/1.jpg');
      newsDiv.appendChild(img);

      var content = document.createElement('content');
      var person = document.createElement('a');
      person.setAttribute('class', 'person');
      person.innerText = 'Lubomyr Marshal';
      content.appendChild(person);
      var br = document.createElement('br');
      content.appendChild(br);
      var comment = document.createElement('comment');
      comment.innerText = text;
      content.appendChild(comment);
      var br = document.createElement('br');
      content.appendChild(br);
      var settings = document.createElement('settings');
      var comment = document.createElement('a');
      comment.innerText = 'Comment';
      settings.appendChild(comment);
      var share = document.createElement('a');
      share.innerText = 'Share';
      settings.appendChild(share);
      var remove = document.createElement('a');
      remove.innerText = 'Remove';
      settings.appendChild(remove);

      content.appendChild(settings);

      newsDiv.appendChild(content);

      var br = document.createElement('br');
      br.setAttribute('style', 'clear:both');
      newsDiv.appendChild(br);

      $('#news').prepend(newsDiv);
  }
 
Share this answer
 
Comments
AspDotNetDev 28-Jun-13 14:34pm    
FYI, you don't have to set "innerText". You can instead append a child that you create using createTextNode.
Lubomur 2-Jul-13 16:17pm    
AspDotNetDev, sory I don't understand, can you wite some code, like
var person = document.createElement('a');
person.setAttribute('class', 'person');
??????//person.innerText = 'Lubomyr Marshal';
Моя тобі порада задавай такі питання ТУТ.А по питанню спробуй використати appendChild()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900