How to add elements to existing HTML using DomDocument?

I have the following select drop-down

<select name="set_login_redirect" id="set_login_redirect">
    <option value="dashboard">WordPress Dashboard</option>
    <option class="level-0" value="1881">Activate</option>
    <option class="level-0" value="2109">Affiliate Area</option>
    <option class="level-0" value="429">Cart</option>
    <option class="level-0" value="430">Checkout</option>

How do I use the PHP DomDocument class to add a new option element to the select drop-down.

For example, I want to add the code below to the HTML above.

How can it be done?

<option class="level-0" value="27">The Word</option>

with the DOM methods, i.e. createElement(), setAttribute(), appendChild().

it might be a bit easier with SimpleXML if the snippet itself conforms to the XML rules.

Lets be a little more specific - you can do this during Execution Time. Once the page has been rendered to the browser, you’re in Javascript’s territory unless the page is reloaded. During execution time, absolutely it’s doable with DOM/XML methods. Also string methods. Quite possibly array methods as well, depending on how the original HTML is constructed.

Adding the new option on the client-side via JavaScript is out of the equation in what am trying to do.

I eventually solved the ish using regex.

we never said you should use JavaScript.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.