473,405 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Functions within the style="" attribute

144 100+
I hope this is just a syntax issue, but I can call a function when a page is served up like this:
Expand|Select|Wrap|Line Numbers
  1. <%
  2. Function FFcolor(x)
  3.     Select case x
  4.     case 1: FFcolor = "#FF0000"
  5.     case 2: FFcolor = "something else"
  6.   etc.
  7. End Function
  8. %>
which called from my element "<a" within the style attribute like this
Expand|Select|Wrap|Line Numbers
  1. <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
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function FFcolor(x) {
  3.     switch (x) {
  4.             case 1:  return "#FF0000";
  5.             case 2:  return "something else";
  6.    etc. }
  7. }
  8. </script>
from inside the
Expand|Select|Wrap|Line Numbers
  1. <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?
Jan 14 '12 #1

✓ answered by Dormilich

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).

4 6584
Dormilich
8,658 Expert Mod 8TB
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).
Jan 14 '12 #2
C CSR
144 100+
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.
Jan 14 '12 #3
you can use by using below concept :

Expand|Select|Wrap|Line Numbers
  1. style="color: '+ fnGetPermission(name)+'"
  2.  
  3.  
  4. function fnGetPermission(name){
  5.         var permissionArray = localStorage.getItem("permissionArray").split(',');
  6.         if($.inArray(permissionArray) == true) {
  7.             return "red;
  8.         } else {
  9.             return "blue";
  10.         }
  11.         }
Dec 27 '17 #4
Calling a JS function from Server side method is not possible.
Feb 1 '18 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Harry Pehkonen | last post by:
I have been defining new class methods when I'm trying to simplify some code. But I'm thinking I should just define functions within that method because they aren't useful from the outside anyway....
2
by: D. Alvarado | last post by:
Hello, I am outputting HTML from an XSLT and I am having trouble setting the value of a form-action field based on certain conditions. Here is the non-working code: <xsl:template...
0
by: hazz | last post by:
I would like to have an attribute stand as a gatekeeper to a method. What I don't want to do is hard code a user's role into the attribute as follows. public void DoSomething(){ //only users...
22
by: John Salerno | last post by:
I might be missing something obvious here, but I decided to experiment with writing a program that involves a class, so I'm somewhat new to this in Python. Anyway, what is the best way to create...
0
by: Mike | last post by:
Hi. I have a vb.net/asp.net application, originally created in VS2003. I'm upgrading it to VS2005, and have found that Public Functions in a module are no longer accessable in my application. ...
2
by: wiggy | last post by:
Hello! I am new to this site and very new to C programming. I am writing a basic GUI in Turbo C which I can do with relative ease. However, our teacher has given us a C file whichwhen run, can...
3
by: dgunjal | last post by:
Is there any method by which i can invoke C++ class functions within C functions?
1
by: kaens | last post by:
So, I have a class that has to retrieve some data from either xml or an sql database. This isn't a problem, but I was thinking "hey, it would be cool if I could just not define the functions for...
2
by: =?Utf-8?B?SnVsaWFu?= | last post by:
My first problem is: I am having an issue when using a web service. The web service is written by another department and say its running fine but when I call it, I sometimes I get a...
9
by: arnaudk | last post by:
Is it possible for a constructor or a function within it to throw an exception? When I declare an object within a try { } block to subsequently catch any exceptions the constructor or functions...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.