Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic creation of variable to apply a style

Status
Not open for further replies.

bradoirs

Technical User
Jul 8, 2009
35
GB
Very new to Javascript and trying to run before I can walk but I am trying to use a javascript routine to change styling dynamically - I have a number of <Div> with generated IDs pix1, pix2 pix3 etc and images with an onclick function example hidepix(1), hidepix(2) etc. I am trying to write a function that that takes the value, generates a variable and applies styling to that Div

function hidepix(x){
var px="pix"+x;
px.style.display="none";
}

But it isn't working - any ideas?

So, in other words, how do I dymanically create a variable that I can apply a style to?
 
Hi

Like this ?
JavaScript:
[b]function[/b] [COLOR=orange]hidepix[/color][teal]([/teal]x[teal])[/teal]
[teal]{[/teal]
    document[teal].[/teal][COLOR=orange]getElementById[/color][teal]([/teal][i][green]"pix"[/green][/i] [teal]+[/teal] x[teal]).[/teal]style[teal].[/teal]display [teal]=[/teal] [i][green]"none"[/green][/i][teal];[/teal]
[teal]}[/teal]

Feherke.
feherke.ga
 
Just as a matter of interest I tried to apply a max-width using same method but without success - any suggestions please?

document.getElementById("pixb" + i).style.max-width="20%";
 
For style properties using javascript that have a '-' word separator you remove the '-' and capitalise the letter following the dash.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top