0

I am trying to understand something. I get a string of html code by doing the following

var formDataUnformatted = $("#content").html();

If I output formDataUnformatted I get something like the following

<div class="form-group ui-draggable ui-draggable-handle element" data-type="text"><div class="close">×</div>
    <label for="text_input" class="control-label col-sm-4">Text Input</label>
    <div class="controls col-sm-7">
        <input id="text_input" class="form-control" name="text_input" type="text">
    </div>
</div>

Now with the above, I need to remove some classes, and get rid of a div. I essentially need to turn it into the following

<div class="form-group">
    <label for="text_input" class="control-label col-sm-4">Text Input</label>
    <div class="controls col-sm-7">
        <input id="text_input" class="form-control" name="text_input" type="text">
    </div>
</div>

To do this, I do the following

var cleanFormData = formDataUnformatted.replace(/\t/, "").replace(/ ui-draggable| element/gi, "").replace(/<div class="close">.<\/div>/g, "").replace(/ data-(.+)="(.+)"/g, "");

Now this pretty much does everything I need it to do, but there is one thing I do not understand. If I output cleanFormData, I get the following

<div class="form-group-handle">
    <label for="text_input" class="control-label col-sm-4">Text Input</label>
    <div class="controls col-sm-7">
        <input id="text_input" class="form-control" name="text_input" type="text">
    </div>
</div>

Now this is perfect, apart from where it adds -handle to form-group. I do not want form-group-handle, I just want it to be form-group.

Where is it getting this -handle from?

Thanks

4 Answers 4

2

Don't parse HTML with regular expressions, work with DOM nodes instead

var formDataUnformatted = $("#content").clone();

formDataUnformatted.find('.form-group').attr('class', 'form-group');

var html = formDataUnformatted.html();

FIDDLE

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

4 Comments

The problem is, that still has all the classes I am trying to remove.
It doesn't in my Fiddle
When I check the source of the output in your fiddle, I see <div data-type="text" class="form-group ui-draggable ui-draggable-handle element">
Well, yes, I cloned the element so it doesn't affect the DOM, isn't that what you wanted. If you just want to remove the class in the DOM, it's trivial, and you don't need any of this -> jsfiddle.net/3bj6n345/4
1

If you want to change the original element..

var html = $("#content").find(".form-group")
  .attr('class', 'form-group')[0].outerHTML;

  alert(html);

https://jsfiddle.net/uanLqtkm/2/

To remove the .close div as well just put this line above the other one.

$("#content").find(".form-group").find(".close").remove();

If you want to just get the modified html without changing the original..

var html = $("#content").clone().find(".form-group")
      .attr('class', 'form-group')[0].outerHTML;

https://jsfiddle.net/uanLqtkm/3/


If you want to change all of them you can do something like this:

$("#content").find(".form-group").each(function(){
    var html = $(this).attr('class', 'form-group')[0].outerHTML;
    alert(html);
});

5 Comments

This will only get one block of .form-group. What happens if I have more than one block?
it will give you the first one. if you need the second one you can change the [0] to [1] and so on.
What happens if I do not know how many there are? Also, it does not seem to remove the div with the class close.
I will update the code to remove the .close div, but are you changing the element or are you just copying the html?
Just copying the html
1

Change replace(/ ui-draggable-handle| ui-draggable| element/gi, "") to replace(/ ui-draggable| element/gi, "").

Comments

1

It is getting it from ui-draggable-handle. Change your replace() to match the whole thing, not ui-draggable.

Also, it is not advisable to use regular expressions to manipulate 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.