0

I've been looking for a way to use jQuery to translate HTML. For instance, I would like to change

<myTag name1="sally" name2="billy" datingFor="3" />
<myTag name1="jill" name2="tom" datingFor="4" />

to another form such as

<div>SALLY has been dating BILLY for 3 months</div>
<div>JILL has been dating TOM for 4 months</div>

using a jQuery function that executes after the document is ready.

Is there an easy way to do this? I know I can use something like $('#div').html('New Text') but this approach doesn't seem applicable when I have parameters (such as the name or datingFor fields).

4
  • .attr() docs Commented Oct 29, 2013 at 16:19
  • are you getting the myTag elements as xml content or as a string? Commented Oct 29, 2013 at 16:20
  • to which element you want to append the derived values Commented Oct 29, 2013 at 16:24
  • My bad... I think I meant as XML. :( As you can tell, I'm pretty new to this!!! (: Commented Oct 29, 2013 at 16:38

3 Answers 3

2

Try this:

$('myTag').each(function() {
    var $this = $(this),
        name1 = $this.attr('name1'),
        name2 = $this.attr('name2'),
        dating = $this.attr('datingFor');

    $this.replaceWith($('<div/>').text(name1 + ' has been dating ' + name2 + ' for ' + dating + ' months'));
});

Working demo: http://jsfiddle.net/92ctS/ but I had to modify the tags from <myTag/> to <myTag></myTag> to have it work on Firefox.

This works with original tags though: http://jsfiddle.net/92ctS/1/

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

Comments

1

jsFiddle Demo

var myTags = $('myTag');

myTags.each(function(i){
    var name1 = $(this).attr('name1');
    var name2 = $(this).attr('name2');
    var datingFor = $(this).attr('datingFor');
    $('.container').append('<div>' + name1 + ' has been dating '+ name2 + 'for '+ datingFor + ' months</div>');
    //$(this).replaceWith('<div>' + name1 + ' has been dating '+ name2 + 'for '+ datingFor + ' months</div>');
});

If you want to replace the existing myTag with the generated div.Use .replaceWith() instead of .append().

2 Comments

Thanks for the answer! In particular, I like the demo! I'm in the process of integrating your answer into my code and assuming it works I will mark yours as the answer.
Works like a charm! Thanks to all the answers. I looked quite a while on the internet trying to figure it out but all of you were able to clearly demonstrate how to do this!
1

Try

$('myTag').replaceWith(function () {
    var $this = $(this);
    return '<div>' + $this.attr('name1') + ' has been dating ' + $this.attr('name2') + ' for ' + $this.attr('datingFor') + ' months</div>'
})

Update: Since the OP has a string

var string = '<myTag name1="sally" name2="billy" datingFor="3" /><myTag name1="jill" name2="tom" datingFor="4" />';

var list = $(string).map(function () {
    var $this = $(this);
    return '<div>' + $this.attr('name1') + ' has been dating ' + $this.attr('name2') + ' for ' + $this.attr('datingFor') + ' months</div>'
}).get();

$('body').append(list.join(''))

Demo: Fiddle

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.