1

please see the following code

<div class="test" style="color:red">1</div>
<div class="test" style="color:blue">2</div>
<div class="test" style="color:green">3</div>

I need to save this html structure in javascript variable . So that i used the following code

<script>
    $(".test").each(function(){
var otext='<div class="test" style="color:'+$(".test").css("color")+';">'+$(".test").text()+'</div>';
    });
</script>

but in otext i got <div class="test" style="color:redbluegreen">123</div>. Actually what wrong in my code ? How to store all html structure and style of class test using jquery?

the desired out put is var otext =<div class="test" style="color:red">1</div><div class="test" style="color:blue">2</div><div class="test" style="color:green">3</div>;

UPDATE :

what happen if the html structure is lite this ?

<div class="test" style="color:red">1<button clss="test-button" value="remove"/></div>

and here i don't want to save this div structure without <button class="test-button">.

1
  • first get otext variable out of each function then store using otext += '<...>' inside Commented Oct 24, 2016 at 9:50

4 Answers 4

3

You can use .map() to iterate and create an array of string using outerHTML property of each element.

jQuery(function($) {
  var text = $(".test").map(function() {
    return this.outerHTML
  }).get().join('');
  console.log(text)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test" style="color:red">1</div>
<div class="test" style="color:blue">2</div>
<div class="test" style="color:green">3</div>

As per comment

jQuery(function($) {
  var text = $(".test").map(function() {
    var elem = $(this).clone();
    elem.find('.test-button').remove();
    return elem.prop("outerHTML").trim();
  }).get().join('');
  console.log(text)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test" style="color:red">1
  <button class="test-button" value="remove" />
</div>
<div class="test" style="color:blue">2
  <button class="test-button" value="remove" />
</div>
<div class="test" style="color:green">3
  <button class="test-button" value="remove" />
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

thank you . I just update the question .please look .
3

In your context the each loop every time the variable has been reassigned and override the values. Assign as global variable and concat it.

And use $(this) for getting current object

var otext = '';

$(".test").each(function(){
 otext +='<div class="test" style="color:'+$(this).css("color")+';">'+$(this).text()+'</div>';
});

console.log(otext);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test" style="color:red">1</div>
<div class="test" style="color:blue">2</div>
<div class="test" style="color:green">3</div>

Comments

1
var otext = '';

in function

otext+='<div cl......

Comments

1

You should wrap your html inside a div like

<div id="mydiv>
    <div class="test" style="color:red">1</div>
   <div class="test" style="color:blue">2</div>
   <div class="test" style="color:green">3</div>
</div>

Then use the jquery as

var html=$('#mydiv').html();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.