preg replace entire div or div content javascript?

user3455531

I have html code(that i get from an ajax call) in a javascript string that i want to manipulate. If the div coming already exists in the string, i want to remove it or want to change its content.

For example i have this code in a string:

<div id ="message_abc_support" class = "messageclass">
<div class="messageBlockImage2">
</div>
<div id="messageBlockText">
<div id="messagesname">abc</div>
<div id="messagesmsg">123</div></div></div>

<div id ="message_abcd_support" class = "messageclass">
<div class="messageBlockImage2">
</div>
<div id="messageBlockText">
<div id="messagesname">abcd</div>
<div id="messagesmsg">123</div></div></div>

now suppose i get from an ajax call:

<div id ="message_abcd_support" class = "messageclass">
<div class="messageBlockImage2">
</div>
<div id="messageBlockText">
<div id="messagesname">abcdef</div>
<div id="messagesmsg">123456</div></div></div>

how do i replace the other div with the same id? Please note i have this in a string so i cant just remove the div

Benjamin Gruenbaum

I feel like my teacher who told us "Well, I've been teaching them Algebra for 30 years, I figured it's about time they started getting it".

To get to the point: Regular expressions are insufficient in strength to parse arbitrary HTML. There are a lot of edge cases and rules that cause bugs.

There are however ways to otherwise easily solve your problem:

The correct approach is to not have duplicate IDs to begin with. Seriously, there is almost never a good reason to have duplicate IDs. They are by definition unique. Simply do not add IDs to the divs in the first place. If you need to query for them consider for a minute what you're doing: You're querying your visual presentation for business information. Instead, use arrays and objects to represent your actual data and bind them to the presentation (with a closure or explicit mapping for example).

But... you already have this code.. And learning about proper UI structuring can take a lot of time.. And maybe you're not even the one creating this code on the backend.

So, you can use the built in DOM parsing abilities every browser has:

   var el = document.createElement("div"); // placeholder
   el.innerHTML = response; // feed your AJAX response there
   var elms = el.querySelectorAll("[id]"); // select every element with an ID attribute
   for(var i = 0; i < elms.length; i++){
       // check if element with the same ID exists in the document
       if(document.querySelector(elms[i].id)){
           // wipe the ID out, in your code you can do whatever with it
           elms[i].id = "";
       }
   }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

preg replace entire div or div content javascript?

From Dev

Javascript onclick replace entire inner html of div content

From Dev

Javascript. Replace HTML content of a Div

From Dev

Jquery Animating Entire div Content

From Dev

Replace entire div instead of div contents

From Dev

Issue on Loading a Div entire content to New Div

From Dev

Replace content of DIV with jQuery

From Dev

Replace Div Content onclick

From Dev

Replace div content with JQuery

From Dev

jQuery replace div content

From Dev

Search and replace div content with another div content

From Dev

Javascript replace a div's content with a function when active

From Dev

Remove a div by id with preg_replace

From Dev

Replace div content using jQuery

From Dev

Content wrap is not covering entire area of div

From Dev

Wrapping an entire set of dynamic content with a div

From Dev

jquery replace div content with jsp file content

From Dev

How to replace div with another div in javascript?

From Dev

How to replace div with another div in javascript?

From Dev

Insert Div content in Div dynamically By Javascript

From Dev

Anchor to cover entire DIV - but with challenges and no javascript

From Dev

javascript replace multiple div tags

From Dev

Using javascript to alter content of a <div>

From Dev

changing the content of a nav/div with javascript

From Dev

changing the content of a nav/div with javascript

From Dev

Detecting if a content fits in in a div with Javascript

From Dev

Javascript - Change div content with onclick

From Dev

.replaceWith() to replace content in a div for different link elements

From Dev

replace div content with css3 onclick

Related Related

HotTag

Archive