2

I am trying to replace certain values in a div using angular. The problem is that the div content is generated by an outside service so I cant just add ng-model and be done with it. I've tried the following and some various variations with no luck

var o = angular.element(document.querySelector('#maintwo'));
var r = o[0].innerHTML.replace('TWO','REPLACE');
angular.element(document.querySelector('#maintwo')).innerHTML = r;

plunker

1
  • 1
    missing the [0] from the 3rd line Commented Oct 7, 2016 at 14:28

2 Answers 2

2

just remove angular.element() from this line, and it will work as you need it : angular.element(document.querySelector('#maintwo')).innerHTML = r;

Do it , like : document.querySelector('#maintwo').innerHTML = r;

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

Comments

1

if you remove the angular.element() container from the 3rd line it works.

<!DOCTYPE html>
<html ng-app="myApp" ng-controller="myCtrl">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-sanitize.js"></script>
</head>
<body>
<button ng-click="replaceText()">
  replace
</button>

<div id="maintwo">MAIN TWO</div>
<script>
var myApp = angular.module('myApp', ['ngSanitize'])
function myCtrl($scope) {
    $scope.replaceText = function() {

     var o = angular.element(document.querySelector('#maintwo'));
     var r = o[0].innerHTML.replace('TWO','REPLACE');
    document.querySelector('#maintwo').innerHTML = r; 

    }
}
</script>
</body>
</html>    

Comments

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.