I need help please, i need to know how i could update an elements image bases on an image number.
So i need some sort of javascript that would based on number go and look for the image on local machine shared directory and apply that image to that element.
Or if there is a simpler solution would appreciate it.
The image number will change daily,
Attached is my project:
under group 1 i have labeled the first runner id="image1" sectionals.zip (1.4 MB)
Generally, if you are using an Image element in Hype, and want to change it programmatically, you would use JavaScript code that looks like this:
var imageURL = "https://example.com/path/to/image.jpg";
var element = hypeDocument.getElementById("image1");
hypeDocument.setElementProperty(element, "background-image", imageURL);
There's also ways to simply repoint the initial URL for the image when Hype tries to load with the HypeResourceLoad callback; you can search the forums for this term and will find examples.
Given the code you already have, I am guessing the question might be more about how to use something that is outside of the .hyperesources folder?
It really depends on what you mean by "on local machine shared directory?"
Is this still a web server running in the cloud that would have an http/https URL? Or is this something that is run via using a file:/// URL? Or is it you make changes on a local computer, and need a flow to update it on a remote sever? Will the image URLs themselves change, or will they stay the same? (You may also need to worry about local browser caching too).
Maybe you can elaborate a little bit on your use case and desired setup more and I'd be happy to give more specific advice for that.
Thank you for replying.
We have a middleware software that would download the base64 image and store it either on the machine where the templates are, or copy it on a NAS device.
So i need to be able to access that share through Hype.
The Element and dimensions would stay the same it would just be replaced with a new silk.
Everyday and every Race we have new silks, so i need some how based on the silk image number be able to tell Hype that "image1" which is the first runner to have the following silk allocated to it which is silk number 12009. That same runner on the next race would have a different silk allocated to it.
As you can see in the template we change the order of each runner by allocating a position number to each element and that would then move the element to position 1 or 2 or 3.
But i'm trolling the forms to see if there was an examples that could help me understand how to change the image.
I guess if you have software that is automating this part of the process, I'd ask why not name it the same and just directly replace the file in the .hyperesources folder?
Likewise if there's data associated with the images, that could be JSON which is overwritten. Therefore you don't really need any magic on the Hype side; your middleware just needs to know the names.
However, if you don't have much control over this middleware, then you can probably just run some code. My solution is just code for swapping for the image; @MaxZieb's solution is definitely more generic and can help with the data replacements more easily.
I used your code to pull the silks of, i'm trying now to write that silk image to a new ID so that when we pull template into CasperCG we could then just change the image number and it would then update silk to that, but i'm struggling it's not working
var horseimage2 = hypeDocument.getElementById('f16').innerHTML
console.log ('hello'+horseimage2)
var imageURL = "https://www.sahorseracing.co.za/images/Silks/svg/png/"+horseimage2+".png";
console.log (imageURL)
var silk = hypeDocument.getElementById("image1");
hypeDocument.setElementProperty(silk, "background-image", imageURL);
Hmm... that code looks good to me. I notice you have logging; are the logs what you expect? Can you better define not working?
About the only thin I can think of is that the .innerHTML may have more in it than just the image name you think; sometimes styling or other characters can get in the way. You may want to use .innerText instead and also call .trim() on the result. That's just a guess though.
// Each Input has the class name of silk
var silkInputs =document.querySelectorAll('.silk')
for (i = 0; i < silkInputs.length; ++i) {
var horseImageValue = silkInputs[i].value
// Each Input has a name that matches its image number i.e f16
var horseImageName = "image" + silkInputs[i].name
// If the input is not blank then..
if (horseImageValue != "" ){
// Use the value
var imageURL = "https://www.sahorseracing.co.za/images/Silks/svg/png/"+horseImageValue+".png";
// Get the correct image. Each Image has a id that matches is "image" + fnumber, i.e imagef16
var silk = hypeDocument.getElementById(horseImageName);
hypeDocument.setElementProperty(silk, "background-image", imageURL)
}
}