SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict o~~Goatee~~o's Avatar
    Join Date
    Jan 2004
    Location
    North Yorkshire UK
    Posts
    350
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using function parameters

    Hi there

    I'm new to Javascript and what to you use to help create some features on the admin section of my site. Basically, I have a series of buttons to add HTML tags to a textarea which is used to write a news article. Each button calls a function called quicktags() which takes 2 parameters - the type of tag and also the textarea that the tag needs to be added to. There are 2 text areas - one for a short story and an optional one for the full story.

    So an example button would look like this:
    Code:
    <input type="button" name="b" value="b" onclick="quicktags('bold','news_text')" />
    And here is the function code:
    Code:
    <script language="javascript" type="text/javascript">
    <!--
    function quicktags(type,target) 
    {
    	switch(type) 
    	{
    		case 'para':
    			code = '<p></p>';
    			break;
    		case 'br':
    			code = '<br />';
    			break;
    		case 'bold':
    			code = '<strong></strong>';
    			break;
    		case 'em':
    			code = '<em></em>';
    			break;
    		case 'h3':
    			code = '<h3></h3>';
    			break;
    		default:
    	}
            document.addnews.target.value += code;
    	document.addnews.target.focus();
    }
    //-->
    </script>
    The problem which is probably immediately obvious to some of you is that the code at the bottom of the function that actually adds the HTML code to the textarea doesn't work - It trys to add the HTML code to an element in the addnews form called target rather the elements with the name that was passed to the function.

    With me being new to the language I don't know how to resolve it. Any ideas?

    Many thanks

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is probably most asked javascript question ever.

    And the answer is: use square brackets.

    document.addnews[target].value += code;

  3. #3
    SitePoint Addict o~~Goatee~~o's Avatar
    Join Date
    Jan 2004
    Location
    North Yorkshire UK
    Posts
    350
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lol, I knew it would be so simple. Sorry for taking up forum space.

    Many thanks


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
  •