Skip to content

Latest commit

 

History

History
104 lines (73 loc) · 2.31 KB

attr-and-removeattr.md

File metadata and controls

104 lines (73 loc) · 2.31 KB

attr and removeAttr

<< Previous                                                              Next >>

The attr() method sets or returns attributes and values of the selected elements.

When this method is used to return the attribute value, it returns the value of the FIRST matched element.

In case you want to return the attribute value of all matched elements, you must use iterate() together. See start.md for more information.

When this method is used to set attribute values, it sets one or more attribute/value pairs for the set of matched elements.

The removeAttr() method removes one or more attributes from the selected elements.

Syntax: attr

Return the value of an attribute:

$doc->Q([selector])->attr([attribute]);

Set the attribute and value:

$doc->Q([selector])->attr([attribute], [value]);

$html

<img src="img_pulpitrock.jpg" alt="Pulpit Rock" 
width="284" height="213">
Php
<?php
include "../src/webparser.php";
$doc = new WebParser();
$doc->loadHTML($html);

$doc->Q("img")->attr("width", "500px");

$doc->output();
Output
<img src="img_pulpitrock.jpg" alt="Pulpit Rock" 
width="500px" height="213">

Syntax: removeAttr

$doc->Q([selector])->removeAttr([attribute]);

$html

<p style="font-size:120%;color:red">This is a paragraph.</p>
<p style="font-weight:bold;color:blue">This is another paragraph.</p>
Php
<?php
include "../src/webparser.php";
$doc = new WebParser();
$doc->loadHTML($html);

$doc->Q("p")->removeAttr("style");

$doc->output();
Output
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

Example test code snippet

Click here to go to example test.