Reputation: 10218
Here is my code:
localStorage.setItem('qandaInputsValue7', $("#ask_q_tags span:first-child, #ask_q_tags span + span").each(function() { return $( this )[0].outerHTML }));
The result is:
As you can see It is an object which isn't useful.
The expected result is: (which should be as the value of that key in localStorage)
<span>something</span><span>something else</span>
See? I need to make a combined string of return $( this )[0].outerHTML
to set it into localStorage. How can I do that?
Noted that neither JSON.stringify()
nor .get()
won't work.
Upvotes: 0
Views: 77
Reputation: 66103
.each()
only performs the iterative logic, and does not return anything. What you are looking for is to use .map()
and combine it with .get()
to retrieve the actual output. Finally, you will need to convert the returned array into JSON so that it can be stored as plain text:
var htmlToStore = JSON.stringify(
$("#ask_q_tags span:first-child, #ask_q_tags span + span")
.map(function() {
return this.outerHTML;
})
.get()
);
localStorage.setItem('qandaInputsValue7', htmlToStore);
Note that instead of using $(this)[0].outerHTML
to refer to the original DOM node, you can just use this.outerHTML
:) the htmlToStore
will be an array, so if you want a true string, you will have to collapse the array using .join('')
.
Upvotes: 3
Reputation: 68393
Use map
instead of each
, and then get
and join
localStorage.setItem('qandaInputsValue7', JSON.stringify(
$( "#ask_q_tags span:first-child, #ask_q_tags span + span" ).map( function() {
return $( this )[0].outerHTML
}).get().join(""); //get and join here
));
No need to use JSON.stringify
localStorage.setItem('qandaInputsValue7',
$( "#ask_q_tags span:first-child, #ask_q_tags span + span" ).map( function() {
return $( this )[0].outerHTML
}).get().join(""); //get and join here
);
Upvotes: 1