user3077503
user3077503

Reputation:

using jquery to get the content of a <tag>

I have a question regarding "unknown" tags. I mean tags that are not defined in HTML.

See the following block:

<div class='container'>
  <element definition>
</div>

And then using jquery I want to get the list of all div's children and then extract their content. To achieve this, I don't know if I have only RegExp to do, or there are some jQuery functions to get.

For instance, defining a function set_tags_to_p() that changes the tags to <p>.

Upvotes: 0

Views: 67

Answers (4)

Jeremy Battle
Jeremy Battle

Reputation: 1638

If you know the name of the tags you want to replace, you could just do:

<div class="container">
    <up>Test</up>
    <down>Test</down>
    <left>Test</left>
    <right>Test</right>
    <broken>Test</broken>
</div>

$(function(){
  $('.container').children("up, down").replaceWith(function() {
    return '<p>' + $(this).html() + '</p>';
  });      
});

http://jsfiddle.net/JqY5R/

Upvotes: 0

j08691
j08691

Reputation: 207900

$('.container').find('*').each(function () {
    $(this).replaceWith('<p>' + $(this).html() + '</p>')
})

jsFiddle example

Upvotes: 1

Orlando
Orlando

Reputation: 9692

You can do something like this

var element, elementHtml, pTag;
element = $("element") // you can change the name
elementHtml = element.html();
pTag = $("<p>").html(elementHtml);

Upvotes: 0

dfsq
dfsq

Reputation: 193271

You can replace children elements with p pretty easy like this:

$('.container').children().replaceWith(function() {
    return '<p>' + $(this).html() + '</p>';
});

Demo: http://jsfiddle.net/F73cS/

Upvotes: 1

Related Questions