jquery - Create HTML from JavaScript for each element -
i've run problem , i'm pretty sure way fix requires javascript. i'm not @ javascript; why i'm asking help.
how can write content div
based on html code has been written?
example
dynamic html created javascript based on 'static html' selector called data-dynamic-content='yes'
created javascript:
<ul> <li><a href="#a" class="hvr-bubble-right">webpage settings</a></li> <li><a href="#b" class="hvr-bubble-right">block 1</a></li> <li><a href="#c" class="hvr-bubble-right">block 2</a></li> </ul>
static html:
<div id="a" data-dynamic-content='yes' class='hidden' data-name='webpage settings'> <!-- content --> </div> <div id="b" data-dynamic-content='yes' class='hidden' data-name='block 1'> <!-- content --> </div> <div id="c" data-dynamic-content='yes' class='hidden' data-name='block 2'> <!-- content --> </div>
to generate list items based on amount of divs found in static html provided, loop on divs class = 'hidden'.
for each of divs add listitem new variable. add list items result div.
var html = ""; $('div.hidden').each(function(){ html += "<li><a href='#" + $(this).attr('id') + "' class='hvr-bubble-right'>" + $(this).data('name') + "</a></li>"; }); $('#result').html("<ul>" + html + "</ul>");
<ul> <li><a href="#a" class="hvr-bubble-right">webpage settings</a></li> <li><a href="#b" class="hvr-bubble-right">block 1</a></li> <li><a href="#c" class="hvr-bubble-right">block 2</a></li> </ul>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="a" data-dynamic-content='yes' class='hidden' data-name='webpage settings'> <!-- content --> </div> <div id="b" data-dynamic-content='yes' class='hidden' data-name='block 1'> <!-- content --> </div> <div id="c" data-dynamic-content='yes' class='hidden' data-name='block 2'> <!-- content --> </div> <div id='result'></div>
i've added result div show generated unordered list.
Comments
Post a Comment