{"id":4686,"date":"2025-09-22T17:44:17","date_gmt":"2025-09-22T16:44:17","guid":{"rendered":"https:\/\/ibex.tech\/wordpress-sites\/?p=4686"},"modified":"2026-04-23T16:26:26","modified_gmt":"2026-04-23T15:26:26","slug":"alter-img","status":"publish","type":"post","link":"https:\/\/ibex.tech\/wordpress-sites\/page-builders\/elementor\/javascript\/alter-img","title":{"rendered":"Alter .img src"},"content":{"rendered":"\n<p>Use querySelector to target the img directly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>document.querySelector('#MyCssId img').src = 'myimage.png';\ndocument.querySelector('#MyCssId img').srcset = '';    \/\/This is needed if Elementor is applying the srcset= parameter\n\n\/\/If you want to apply a link (needs to already have a href):\ndocument.querySelector('#MyCssId a').href = imgUrl;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Why document.getElementById(&#8216;MyCssId&#8217;).src doesn&#8217;t work<\/h4>\n\n\n\n<p>As for <a href=\"\/wordpress-sites\/page-builders\/elementor\/javascript\/alter-textcontent\">.text<\/a>, when you apply an ID to an Elementor widget, it is often applied a level up from the actual displayed image<\/p>\n\n\n\n<p>If you use this on the ID:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>document.getElementById('MyCssId').src = 'https:\/\/somedomain.com\/myimage.png';<\/code><\/pre>\n\n\n\n<p>You won&#8217;t update it. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use querySelector to target the img directly: Why document.getElementById(&#8216;MyCssId&#8217;).src doesn&#8217;t work As for .text, when you apply an ID to an Elementor widget, it is often applied a level up from the actual displayed image If you use this on the ID: You won&#8217;t update it.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[385],"tags":[],"class_list":["post-4686","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/posts\/4686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/comments?post=4686"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/posts\/4686\/revisions"}],"predecessor-version":[{"id":4797,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/posts\/4686\/revisions\/4797"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/media?parent=4686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/categories?post=4686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/wordpress-sites\/wp-json\/wp\/v2\/tags?post=4686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}