Here is a textarea with some content displayed:
<textarea id="textfield"><img src="smile.png alt=":)"/>Hi</textarea>
I have implemented this JavaScript code snippet to extract the img alt value and the text Hi from the textarea in order to rearrange them as :) Hi. However, my goal is to arrange them in the reverse order like Hi :), using the following snippet:
$scope.performaction = function () {
//get the value of the textarea
var textarea = angular.element('#textfield').val();
textareaValue = textarea;
var altValues = [];
while (true) {
var altValueMatch = textareaValue.match(/\<img.*?alt=(\"|\')(.*?)\1.*?\>/),
altValue = (Array.isArray(altValueMatch) && typeof altValueMatch[2] === "string")
? altValueMatch[2]
: null;
if (altValue !== null) {
altValues.push(altValue);
} else {
break;
}
textareaValue = textareaValue.replace(/\<img.*?\>/, "").trim();
}
var concatenated = [altValues, textareaValue].join(" ");
concatenated.replace(/ |,/g,'');
//assign the value to the second textarea of ng-model="content"
$scope.content = concatenated;
};
I need help in modifying the above JavaScript code so that the retrieved textarea value can be rearranged to display as Hi :) after processing it with AngularJS. Basically, I want the image alt value to appear last.