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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing item from an HTML document to JS 1

Status
Not open for further replies.

GulfImages

Technical User
Jul 9, 2004
60
US
Hello All,

I have a shopping cart I'm working on for my website and I have a table with 2 cells side-by-side. The first cell has a picture, the second a form with print sizes, pricing etc.. I decided to build this table with JS because I have some pricing and discount structure in a JS file that I can change, just in that file, and update all my gallery pages.

In my JS file I have to items that I would like to reference a file name from the HTML page but cannot figure out how to do it.

Here are the items:

'<img id="imgborder" src="1771.jpg" width="450" height="300">',

I would like to set a variable or whatever in my html for the name of the file "1771.jpg" and reference it into the <img> tag shown above. Can it be done?

I also have this function in my JS:

'SetImg (\'1771_thm.jpg\');',

I would like to be able to reference the name of that file "1771_thm.jpg" from my HTML page into my script.

Can I declare a variable in the HTML page than reference it into the above statements?

Thanks,
Bobby
 
I may have not been clear in my post above. The JS is in a seperate file from the HTML and called using the src="MyJS" statement.
 

GulfImages said:
Can I declare a variable in the HTML page

Yes you can - but only using JavaScript... HTML does not allow you to define variables.

GulfImages said:
I would like to set a variable or whatever in my html for the name of the file "1771.jpg" and reference it into the <img> tag shown above. Can it be done?

You can alter image properties (including the src attribute) using JavaScript as well - for example with getElementById. So for your example above, to change the src of the image with id "imgborder", you would use:

Code:
document.getElementById('imgborder').setAttribute('src', 'newValue.gif', 0);

Hope this helps,
Dan

 
Thanks Dan,

It does help, but say I wanted to store the name of newValue.gif somewhere in my html doc and insert it into the script you show? Would I make a variable using JS in the head of my html? even if it is a string, I just need to get the text of the file name into my JS.

Thanks.
 
I can make the JS work perfectly from within the HTML but if I want to change a price or add a new print size or frame or other product to my dropdown list, I have to re-upload all the html files for each pic. (Could be 500-600 pages).

I'm trying to seperate the JS from the HTML, which works, I just need to figure out how to feed the name of the file into my code shown above. I'm terrible at explaining things so I'm just trying to make it clearer.
 

I'll not answer any more in this thread, as you have started another thread for much the same query - and multiple threads just get confusing for anyone trying to help.

Dan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top