SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert <img> tag into Textarea

    Hi

    I'm writing a CMS and I need some help coming up with some JavaScript Code which will allow a user to click on an image shown on the page and this action will cause the following HTML snippet to be written into a textarea field named "content":

    <img src="strImagePath" />

    Where strImagePath is the src attribute of the image which was clicked.

    The snippet should be inserted after any text already present in the textarea field.

    The images are displayed in a <div> tag which can be shown or hidden the using the following HTML:

    Code:
    <div id="imagebrowser">
    <img src="images/image1.jpg" name="image1" />
    <img src="images/image2.jpg" name="image2" />
    <img src="images/image3.jpg" name="image3" />
    </div>
    Can anyone point me in the right direction?
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think this should do what your after, let me know

    Code:
    <script type="text/javascript" charset="utf-8">
    	var imageAdder = function(){
    		var browser = document.getElementById('imagebrowser');
    		var images = browser.getElementsByTagName('img');
    		
    		//Add click event to all images within image browser
    		for(var i=0;i<images.length;i++){
    			images[i].onclick = update;
    		}
    		
    		//function to update the textarea
    		function update(){
    			var content = document.getElementById('content');
    			content.value = content.value + ' <img src="'+this.src+'" />'
    		} 
    	}
    	
    	window.onload = function(){
    		imageAdder();
    	}
    </script>

  3. #3
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mortimer, that worked brilliantly!
    I owe you a pint!
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Woo hoo! I like working for beer

    glad it worked.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •