1

I am trying to edit html content based on style tags

Here is the section of html:

<!DOCTYPE html>
<html>
  <body>
    <span style="color:restest">Testing</span>
    <br>
    <span style="color:restest">Testing Again</span>
  </body>
</html>

I want to modify all instances of spans with style="color:restest" and add html tags to the contents.

They should become something like:

<a href="example.com/testing">Testing</a>
<a href="example.com/testing_again">Testing_Again</a>
2
  • Possibly duplicate with stackoverflow.com/questions/3841850/… Commented Dec 5, 2017 at 16:36
  • use $('span').css({color: 'restest'}) simply Commented Dec 5, 2017 at 16:37

1 Answer 1

2

The easiest way to do this would be with jQuery#replaceWith:

$('span[style="color:restest"]').each((i,e) => {
  $(e).replaceWith(`<a href="example.com/${e.innerHTML}">${e.innerHTML}</a>`);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span style="color:restest">Testing</span>
<span style="color:restest">Testing Again</span>

Sign up to request clarification or add additional context in comments.

4 Comments

Why are you using document.querySelectorAll instead of $('span[style="color:restest"]')
@SethMcClaine reason being that i prefer vanilla js over jquery whenever i can use it. However i see your point and i have updated the answer.
I agree, but tend to try to stick with one tech instead of jumping back and forth. Gave you an up though :-)
This is perfect! Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.