Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts

    Question Converting variable to argument?

    Hi,

    I hope I've got my terminology correct here but if not please bare with me.

    Is it possible without using a huge switch statement to do something like the following:

    Pass the element id, required style type and style definition to the function

    Code:
    <div id="test">
         <a href="javascript:fAddStyle('test', 'color', 'red');">Click here</a>
    </div>
    And then somehow covert the variable "color" to the JavaScript argument .color

    Code:
    function fAddStyle(sElementId, sStyle, sAttribute) {
    	// Missing conversion of variable //
    	document.getElementById(sElementId).style.sStyle = sAttribute;
    }
    i.e I can use one small function to convert any style to any attribute and not have a big switch statement covering off every possible .style type that may come up.

    Any pointers to what I should read to improve my terminology in this area would be appreciated too
    Last edited by mattyod; 09-27-2008 at 06:31 PM. Reason: code correction

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    document.getElementById(sElementId).style[sStyle]=sAttribute;
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    mattyod (09-27-2008)

  • #3
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    aah you beauty Nice and simple.

    I always seem to have a bit of trouble with my []s and using them correctly.


  •  

    Posting Permissions

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