Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Object Properties in .js files not properly executing in HTML

I'm very new to JavaScript (I have very limited Python experience as well) and am trying to find a simple way to call an object property from another .js file I have feeding into my HTML base file.

Snip of flowers.js

const rose = {name:'Rose', color:'Red', pic:'Rose.png'};

Snip of HTML base file

<html>
<head> 
<script src="flowers.js"></script>
</head>
<body>
<img id='flowerOne' src=rose.pic style='width:100px'>
</body>
</html>

This works fine if I paste the image reference in directly ("Rose.png" within img src) but not if I write out rose.pic, which theoretically has the exact same reference? I've managed to get .js file functions to work just fine, it just seems to be calling object properties where I'm struggling.

I'm probably not understanding something - any help is appreciated. Thank you!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can change the source of the image in javascript. Just use this

flowers.js

 const rose = {name:'Rose', color:'Red', pic:'Rose.png'};
 document.getElementById('flowerOne').src = rose.pic

HTML:

<html>
<head> 
</head>
<body>
<img id="flowerOne" style='width:100px'>
<script src="flowers.js"></script>
</body>
</html>
about 4 years ago · Juan Pablo Isaza Report

0

One approach is to use JavaScript to update the src attribute of the html img. You can accomplish that by first selecting the image as follows:

let img = document.querySelector('img');

Then you can assign it a new src attribute value as follows:

img.src = rose.pic;

The image can have an empty src attribute, a placeholder image, or no src attribute at all. Your choice.

Learn more about the querySelector method

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!