Feather: Dynamic change one icon to another

Created on 11 Jun 2018  路  6Comments  路  Source: feathericons/feather

How to programatically (dynamically) change icon to another?
E.g. to "watch"?

question

Most helpful comment

If you have jQuery, this seems to work:

$(".parent svg.feather.feather-circle").replaceWith(feather.icons.square.toSvg());

Essentially - find the existing Feather-generated SVG element and replace it with a new one using the Feather JS function.

All 6 comments

I'm not sure I know what you mean. Could you find an example of doing this with another icon set?

e.g. https://fontawesome.com/how-to-use/svg-with-js
search for "Changing icons by changing class"

<button>Open up <i class="fa fa-angle-right"></i></button>

<script>
  document.addEventListener('DOMContentLoaded', function () {
    $('button').on('click', function () {
      $(this)
        .find('[data-fa-i2svg]')
        .toggleClass('fa-angle-down')
        .toggleClass('fa-angle-right');
    });
  });
</script>

clicking a icon button changes that icon to another (here, an arrow)

If you have jQuery, this seems to work:

$(".parent svg.feather.feather-circle").replaceWith(feather.icons.square.toSvg());

Essentially - find the existing Feather-generated SVG element and replace it with a new one using the Feather JS function.

@craigrodway's solution should work. If it works for you, feel free to close this issue.

@craigrodway Or without jQuery:

document.querySelector('.parent svg.feather.feather-circle').replaceWith(feather.icons.square.toSvg())

The answer to this question is quite elaborative to explain.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

naoufel-ui picture naoufel-ui  路  3Comments

kurisubrooks picture kurisubrooks  路  3Comments

tmcgann picture tmcgann  路  3Comments

designgrill picture designgrill  路  5Comments

kurisubrooks picture kurisubrooks  路  4Comments