1

I've loaded some html using DOMDocument as follows:

$dom = new DOMDocument();           
$dom->loadHTML($somehtml);
foreach($dom->getElementsByTagName('*') as $element ){
  if($element->getAttribute("type")=='radio') {
    // I need to set the radio group checked value
  }
}

There are 3 radio buttons in each group, all have the same name per group. How can I set a particular radio button's checked property to true? I assume I need to access a collection some how?

Nick W
  • 877
  • 2
  • 15
  • 30
  • there are documentation here that may be useful: http://www.nusphere.com/kb/phpmanual/function.dom-domelement-setattribute.htm – unixmiah Mar 24 '16 at 16:11

2 Answers2

0

See the answer here... How to select a radio button by default?

<input type="radio" name="imgsel"  value="" checked="checked" />
Community
  • 1
  • 1
ThrowBackDewd
  • 1,737
  • 2
  • 13
  • 16
  • This is fine for regular static html, I need to set the checked property dynamically in Php using the domDocument. – Nick W Mar 24 '16 at 23:49
0

Here is how I do it.

$radio_10 = $html->createElement('input');
$radio_10->setAttribute('onclick', 'ltgGetFire(false);');
$radio_10_attribute = $html->createAttribute('type');
$radio_10_attribute->value = 'radio';
$radio_10->appendChild($radio_10_attribute);
$radio_10_attribute = $html->createAttribute('name');
$radio_10_attribute->value = 'radius';
$radio_10_attribute = $html->createAttribute('checked');

$radio_10->appendChild($radio_10_attribute);
$radio_10_attribute = $html->createAttribute('id');
$radio_10_attribute->value = 'radius_10';
$radio_10->appendChild($radio_10_attribute);