2 Replies - 673 Views - Last Post: 16 September 2012 - 11:52 AM

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Why Isn't This Function For Inserting Text Into A Textarea Not Wor

Posted 14 September 2012 - 10:40 PM

So I have a textarea named CommentReview and I was going to using

function InsertHyperlink()
{
   var link = prompt("Please Enter A Link Location");
   document.getElementsByName("CommentReview").value = link;
}


And it's called within a button click like so

<input type="button" name="Hyperlink" value="Hyperlink" onclick="javascript:InsertHyperlink();" />



The prompt shows but it does absolutely nothing. What could be wrong? It also doesn't work with other text elements in the page either.... Thanks guys

Is This A Good Question/Topic? 0
  • +

Replies To: Why Isn't This Function For Inserting Text Into A Textarea Not Wor

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,441
  • Joined: 08-June 10

Re: Why Isn't This Function For Inserting Text Into A Textarea Not Wor

Posted 15 September 2012 - 02:38 AM

getElementsByName() gives you a Collection of elements (similar to an array), even if there is only one element (or none) inside. hence you must access the element(s) first to modify it.
// if there is an element
document.getElementsByName("CommentReview")[0].value = link;


PS. such collections have two properties:
- length (the number of elements in that collection)
- item() (accessor for an indexed element, stems from the DOM definition and is hardly used in JS as you can use Array access as well)
Was This Post Helpful? 2
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Why Isn't This Function For Inserting Text Into A Textarea Not Wor

Posted 16 September 2012 - 11:52 AM

+++ That works thanks Dormilch. I didn't know that's how objects are accessed now this is easy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1