vlovystack
vlovystack

Reputation: 703

Increase value in multiple lines at the same time

Say I have 500 objects (lines) like this:

<object id="5700" interior="0" doublesided="false" model="1890" dimension="0" posX="400" **posY**="30" posZ="100" rotX="0" rotY="0" rotZ="0"></object>

Now for the entire "block" of 500 lines, I'd like to add 20 to posY.

How would I do this?

Upvotes: 1

Views: 33

Answers (2)

ebcode
ebcode

Reputation: 306

You can use the SimpleXMLElement class to create an object with parameters you can modify.

Try this in a php file as an example to play around with:

$xml_fragment = '<object id="5700" interior="0" doublesided="false" model="1890" dimension="0" posX="400" posY="30" posZ="100" rotX="0" rotY="0" rotZ="0"></object>';

$XML = new SimpleXMLElement($xml_fragment);

for ($i=0; $i<500; $i++){
    $XML['posY']=$XML['posY']+20;
    echo $XML->asXML(); 
}

In your case, you would be looping through your existing objects, creating a new $XML object for each string, adding 20 to the 'posY' parameter of the object, and outputting to wherever it needs to go.

Upvotes: 0

sridhar
sridhar

Reputation: 622

You can do this using multiple ways, one by adding a common selector like class or attr, or by using getElementsByTagName('object') and loop through the objects and set the attribute with the changed values.

Look at the below code for an example.

const myObjects = document.querySelectorAll('.myObject');

myObjects.forEach( object => {
  let currentPosX = object.getAttribute('posX');
  object.setAttribute('posX', parseInt(currentPosX, 10) + 20)
})

console.log(myObjects);
<object id="5700" class="myObject" interior="0" doublesided="false" model="1890" dimension="0" posX="400" **posY**="30" posZ="100" rotX="0" rotY="0" rotZ="0"></object>
<object id="5701" class="myObject" interior="0" doublesided="false" model="1890" dimension="0" posX="400" **posY**="30" posZ="100" rotX="0" rotY="0" rotZ="0"></object>
<object id="5702" class="myObject" interior="0" doublesided="false" model="1890" dimension="0" posX="400" **posY**="30" posZ="100" rotX="0" rotY="0" rotZ="0"></object>
<object id="5703" class="myObject" interior="0" doublesided="false" model="1890" dimension="0" posX="400" **posY**="30" posZ="100" rotX="0" rotY="0" rotZ="0"></object>

Upvotes: 1

Related Questions