1

On my page i have a couple of tags that looks like this:

<parameter name="address" class="transform">@ViewBag.Address</parameter>

To do that i have created this function:

 function transform(id) {
     $('#' + id + ' .transform').each(function (index) {
         var tag = $(this).parent().html();
         $(this).parent().html() = tag.replace('parameter','input type="text"');
     });
 }

Now, if i alert $(this).parent().html() i get the original tag, but the function doesn't work. What am i doing wrong?

3
  • $(this).parent().html() = "value" call is wrong. Have a look to example in the API api.jquery.com/html Commented Apr 21, 2012 at 19:58
  • No, the selector says that it should loop through every DOM-element with the class 'transform' within the element (in singular) with the specified ID. Commented Apr 21, 2012 at 19:58
  • @AntonGildebrand - Since I was unable to get the answer you chose to work, I created a solution with a jsFiddle (see below). Commented Apr 21, 2012 at 20:41

2 Answers 2

2

Try this one (give html function a parameter, may be the cause of the problem

function transform(id) {
 $('#' + id + ' .transform').each(function (index) {
     var tag = $(this).parent().html();
     $(this).parent().html( tag.replace('parameter','input type="text"') );
 });
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I guess i got a little confused.
Hmmm, this didn't work for me when I created a fiddle to test it.
1

The answer you accepted didn't work for me so I created this jsFiddle example.

jQuery:

function transform(id) {
    $('#' + id + ' .transform').each(function(index) {
        $(this).replaceWith($('<input value="' + $(this).html() + '" type="text" name="address" class="transform" />'));
    });
}

This will convert the parameter tags into input elements and place the @ViewBag.Address text as the default value for each input.

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.