Functions within the style="" attribute

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • C CSR
    New Member
    • Jan 2012
    • 144

    Functions within the style="" attribute

    I hope this is just a syntax issue, but I can call a function when a page is served up like this:
    Code:
    <%
    Function FFcolor(x)
    	Select case x
        case 1: FFcolor = "#FF0000"
        case 2: FFcolor = "something else"
      etc.
    End Function
    %>
    which called from my element "<a" within the style attribute like this
    Code:
    <a ... style="color: <%=FFcolor(x)%>; ... >
    and the color is set to #FF0000 or something else.

    But I can't find a similar way to call a javascript function like this
    Code:
    <script type="text/javascript">
    function FFcolor(x) {
    	switch (x) {
                case 1:  return "#FF0000";
                case 2:  return "something else";
       etc. }
    }
    </script>
    from inside the
    Code:
    <a ... style="color: [ javascript function call?]; ... >
    I want to change the font depending on the page, but use the same function for events.

    Since I already have functions setup to do this for events (mouseover, etc) I want to use the same functions for initializing a new page (like a refresh). In other words, the color changes from some previous color when the page is presented, conditioned on a "pagename" variable, then changes onmouseover, and changes back to the first color onmouseout (without any onclick action). Each page starts out with its unique font color.

    The mouseover & mouseout, and "I wish" the startup font color results should be based on the same function using the same "switch" subfunction.

    I can use the first example, <%= %> for the startup color but not for the events (because of some further manipulation I do); and that means I have to have similar routines, "switch" and "Select Case" statements, in 2 locations, one for ASP and one for Javascript.

    Repeating, I need some javascript syntax to call from inside the style=" attribute so that the pair color:value is really a color:function. I've tried a lot of ways without any errors but no color actually comes back in javascript. The result should be ... style="color: #FF0000; ... "or some other color"

    Anybody?
    Last edited by Dormilich; Jan 14 '12, 08:53 AM. Reason: please use [CODE] [/CODE] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what you try to do is not possible (with this approach). reason: in the first example you use a server-side script (ASP?). at this point, the whole HTML page is just a string inside the script. JavaScript on the other hand side works on the client and when it is invoked, the HTML source code is already rendered. hence there is no way to influence the output by changing the source code. what you need to do is change the page representation (the DOM tree) the browser generated.

    for changing a CSS property you would have to set/alter the Element’s style object elem.style.color = "red"; (of course you could also use a function’s return value).

    Comment

    • C CSR
      New Member
      • Jan 2012
      • 144

      #3
      Thanks Dormilich. That makes sense. I was trying to re-render the page or something like that and a little bit confused. I got it. Thanks for your time.

      Comment

      • santoshk1
        New Member
        • Dec 2017
        • 1

        #4
        you can use by using below concept :

        Code:
        style="color: '+ fnGetPermission(name)+'"
        
        
        function fnGetPermission(name){
        		var permissionArray = localStorage.getItem("permissionArray").split(',');
        		if($.inArray(permissionArray) == true) {
        			return "red;
        		} else {
        			return "blue";
        		}
        		}

        Comment

        • RockybBalboa
          New Member
          • Feb 2018
          • 5

          #5
          Calling a JS function from Server side method is not possible.

          Comment

          Working...