tags:

views:

72

answers:

4

Ive tried to use the Dom model with no bloody luck, getElementByID just doesnt work for me.

I loathe to resort to a regex but not sure what else to do.

The idea is to replace a <div id="content_div"> all sorts </div> with a new <div id="content_div"> NEW ALL SORTS HERE </div> and keep anything that was before or after it in the string.

The string is a partial HTML string and more specifically out of the wordpress Posts DB.

Any ideas?

UPDATE: I tagged this question PHP but probably should of mentioned Im looking for a PHP solution only.

Update: Code Example

$content = ($wpdb->get_var( "SELECT `post_content` FROM $wpdb->posts WHERE ID = {$article[post_id]}" ));

$doc = new DOMDocument();
$doc->validateOnParse = true; 
$doc->loadHTMLFile($content);   
$element = $doc->getElementById('div_to_edit');

So Ive tried a whole lot of code and this is what Ive got so far, probably not right but Ive been hacking at it for a little while now.

A: 

Try this..

var myDiv = document.getElementById('content_div');
myDiv.innerHTML = "NEW ALL SORTS HERE";
Dr. Frankenstein
I think he is using php's DOMDocument ont he server side... not javascript.
prodigitalson
Im looking for a PHP answer, I tagged it PHP but I probably should of mentioned that as well sorry.
bluedaniel
+2  A: 

You're right: getElementByID doesn't work.

Try getElementById() instead. Javascript is case sensitive.

Robusto
A: 

remember that you must have a valid webpage and that means full HTML tree:

<html>
<head>
<script type="text/javascript">
function changeText(div){
    document.getElementById(div).innerHTML = 'my new text';
}
</script>
</head>
<body>

--- your body ---

<div id="DIV_ID">old text</div>

<input type="button" onclick="chageText('DIV_ID');" value="Click me" />

</body>
</html>

remember that you have to call getElementById with:

<script type="text/javascript"> // as from HTML5 you can use only <script>
    document.getElementById("DIV_ID").innerHTML = 'my new text';
</script>
Mihai Iorga
A: 

ok i assume $content is a snippet? then this may be all you need:

$doc = new DOMDocument();
$doc->validateOnParse = true; 
$doc->loadHTMLFile('<html><body>' . $content . '</body></html>');   
$element = $doc->getElementById('div_to_edit'); 
prodigitalson
it is a snippet, so in your example how would I be able to to replace the div 'div_to_edit' with another div altogether and then save the HTML?
bluedaniel