Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

Just wondering if there's a way to do a calculation in javascript without having forms and inputs in my html.

Basically I have an image on my page that I need to get the width of, and divide by 8, a bit strange I know.

I have managed to get the image width stored as a variable, now all I need is to find a way to divide that variable by 8 without interrupting the layout of the html document.

Can anyone help me!!

Regards,
Tom.
Posted
Comments
Sandeep Mewara 1-May-12 5:17am    
Elaborate: a calculation in javascript without having forms and inputs in my html

You can always do something like, var val = somewidth/8;
Sergey Alexandrovich Kryukov 1-May-12 11:44am    
I think this is clear, not clear why; but apparently because OP is lost very well. (Please see the comment in response to your comment below.)
I basically answered, please see...
--SA
thomasriley 1-May-12 5:26am    
where you say "somewidth" , could I put the name of the variable there? As I have a variable stored which is the width of the image

1 solution

You can divide any variable as much as you want, it won't touch your HTML document, ever. Your layout cannot be "interrupted" (what is it? :-)), but it can be modified only if you assign new values to some elements of your DOM document. You either change the width of your image (by modifying its width property) or not; you cannot requires to change something and change nothing at the same time. The change in width always change some layout, but this change can be limited to small part of the document, which totally depends on your, well… layout. If has nothing to do with JavaScript at all, but depends entirely on your HTML and CSS.

As to your question about forms or inputs: you don't need any forms or input controls to invoke a JavaScript. Any event can be used (click, hover, you name it), including such basic one as the event of loading the page, onload, please see:
http://www.w3schools.com/jsref/event_onload.asp[^].

This is just basic information, only on part related to your question. It won't help you much if you won't learn the very basics of HTML, CSS, JavaScript and their functionality. I feel you really need it, and perhaps from the very beginning.
This is one of the places to start: www.w3schools.com[^].

Additionally, you will need to check with the standards. You can easily find all the documentation.

—SA
 
Share this answer
 
v2
Comments
Sandeep Mewara 1-May-12 13:03pm    
That's what I was confused of! any number of calculation could be made without involvement of html in JS yet he asked something related in a manner that I was not sure of.
Sergey Alexandrovich Kryukov 1-May-12 22:30pm    
It must be just lack of basic understanding of what HTML and JavaScript do.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900