0

I am fetching the user input data from textarea on button click. If a user put some hyperlink code inside textarea, the function automatically recognize and add rel=nofollow. Now I need to assign these new hyperlink code inside textarea again and replace the old one.

I am able to add rel=nofollow and able to alert it

function naming() {
  var rel_attribute = document.getElementById('textareaCode').value;
  var dom = new DOMParser().parseFromString(rel_attribute, 'text/html');
  [...dom.getElementsByTagName('a')].forEach((a) => {
    a.setAttribute('rel', 'nofollow');
    alert(a.outerHTML);
  });
}
<textarea id="textareaCode"></textarea>
<a href="#" onclick="naming()">Execute Function</a>

4
  • to clarify, are you just looking to update the textfield value? i.e user enters html in to the textarea, the function updates the text they entered to have the nofollow? Commented Jun 17, 2019 at 12:38
  • No clue what your problem actually is. Commented Jun 17, 2019 at 12:38
  • What you want to do with the text from the textarea? Commented Jun 17, 2019 at 12:39
  • I only need to update the value of textarea with rel attribute Commented Jun 17, 2019 at 12:40

3 Answers 3

4

Using replace()

function naming() {
  var textarea = document.getElementById('textareaCode');
  var rel_attribute = document.getElementById('textareaCode').value;
  var dom = new DOMParser().parseFromString(rel_attribute, 'text/html');
  [...dom.getElementsByTagName('a')].forEach((a) => {
    let original = a.outerHTML
    a.setAttribute('rel', 'nofollow');
    textarea.value = textarea.value.replace(original, a.outerHTML)
  });
}
<textarea id="textareaCode" cols=70 rows=5>
<a href="#" onclick="naming()">Execute Function</a>
</textarea>
<br/>
<a href="#" onclick="naming()">Execute Function</a>

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

Comments

1

Use the XMLSerializer interface to convert your DOMParser instance into HTML string, then affect your <textarea>'s value property with the HTML string.

You'll need to do some other processes if you want to only have the <body> tags' content.

function naming() {
  const eArea = document.getElementById('textareaCode'),
    dom = new DOMParser().parseFromString(eArea.value, 'text/html');
  
  [...dom.getElementsByTagName('a')].forEach(a => a.setAttribute('rel', 'nofollow'));
  
  // That's here!
  eArea.value = new XMLSerializer().serializeToString(dom);
}
<textarea id="textareaCode"></textarea>

<a href="#" onclick="naming()">Execute Function</a>

Comments

0

Please try out as follows,

let counter = 1;

function naming() {
  var rel_attribute = document.getElementById('textareaCode').value;

  var anchorTags = document.getElementsByTagName('a');

  var dom = new DOMParser().parseFromString(rel_attribute, 'text/html');
  [...dom.getElementsByTagName('a')].forEach((a) => {
    a.setAttribute('rel', 'nofollow');
    
    a.onclick = naming;
    a.innerText = "Manish " + counter++;
    
    for(let i=0; i < anchorTags.length; i++){
      anchorTags[i].remove();
    }
    
    document.body.appendChild(a);

    console.log(anchorTags);
  });
}
<textarea id="textareaCode"></textarea>
<a href="#" onclick="naming()">Execute Function</a>

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.