Okay, so quite a smile question that I am stuck with. I am attempting to make a HTML text box that when submitted, the text is thrown into a function and checked to see if there are any links. If there is a link, it is wrapped in anchor tags and made into a clickable link.
So I got that part working, but I then created an edit function. So when an edit button is pressed on the comment, a popup is shown with the created comment. The value of the comment (taken from an XML file) is placed into the value of this edit text area. But the value of the links still has the anchor tags, so it looks extremely messy.
I am trying to make a function that runs over this comment and removes any anchor tags and just has the remaining text within the anchor tags.
Simple example, when a user posts a comment, with a link like www.stackoverflow.com, it will be saved in my XML document like so:
<a href="www.stackoverflow.com">www.stackoverflow.com</a>
It also works for if the user posts the link with http:// at the beginning.
I am now trying to revert the link to normal text like it was before.
Here are both of my functions. The first one, convertLink
works perfectly. convertLinkEdit
is attempting to revert the process, but I am having no luck.
function convertLink(text) {
var words = text.split(' ');
var newText = '';
for (var i = 0; i < words.length; i++) {
var word = words[i];
if (word.indexOf('http://') === 0) {
word = '<a href=" ' + word + ' ">' + word + '</a>';
} else if (word.indexOf('www.') === 0) {
word = '<a href=" http://' + word + '" >' + word + '</a>';
}
newText += word + ' ';
}
return newText;
}
function convertLinkEdit(text) {
var words = text.split(' ');
var newText = '';
for (var i = 0; i < words.length; i++) {
var word = words[i];
if (word.indexOf('href=') === 0) {
//if index of finds "href=", it means a link is coming up
//Therefore, since everything is split at blank spaces,
//after the next blank space will be the current text that needs saving
}
newText += word + ' ';
}
return newText;
}
Inside my non working function is comments on how I think it should be done, although I am not to sure on how to implement.