I'm trying to replace an older combination of bootstrap, jQuery with bootstrap version 5.0 (and vanilla JavaScript). The HTML remains unchanged:
<div class="col-xl-5 col-lg-5 col-md-5 col-sm-11 card">
<p style="text-align:center; font-weight:bold; ; font-size:1.1em;">Raw Data Preview</p>
<div id="lv_container" class="od-server l-image">
<img class="whatever" name="lv_name" id='lv_image' src='/api/0.1/l_v/image' data-src='/api/0.1/l_v/image' load="resizeImage()">
</div>
</div>
Combining a standard jQuery selector:
img_elem = $('#lv_image');
I reuse img_elem in other functions:
// Example 1:
img_elem.attr("src", img_elem.attr("data-src") + '?' + new Date().getTime());
// Example 2:
var img_width = img_elem.naturalWidth();
var img_height = img_elem.naturalHeight();
var img_container_width = $("#lv_container").width();
var img_container_height = $("#lv_container").height();
// Example 3:
img_elem.width( Math.floor(img_scaling * img_width));
img_elem.height(Math.floor(img_scaling * img_height));
Because img_elem is an [object Object] (i.e. a string version of object instance), I thought I could just find a vanilla JavaScript way of obtaining the same object. But however I try using document.getElement... and document.querySelector, I only get HTMLImageElement, HTMLCollection or NodeList objects. Syntax for these attempts:
let a = document.getElementById('lv_image');
console.log("a: " + a + " " + typeof a);
let b = document.querySelector('#lv_image');
console.log("b: " + b + " " + typeof b);
let c = document.getElementsByClassName('whatever');
console.log("c: " + c + " " + typeof c);
let d = document.getElementsByName('lv_name');
console.log("d: " + d + " " + typeof d);
Web development is not my expertise, I must be missing an obvious term here. How do I obtain the equivalent vanilla JavaScript object or functionality? Is jQuery really so radically different that there is no joint solution for this and I need to find a separate solutions for Example 1, Example 2 and Example 3?
-- Edit1 --
Part of my confusion is/was, understanding the syntax of JavaScript/jQuery. I was puzzled that the jQuery API didn't document the naturalWidth(), naturalHeight() functions. The reason for this is, my inherited code contained the following functionality:
var
props = ['Width', 'Height'],
prop;
while (prop = props.pop()) {
(function (natural, prop) {
$.fn[natural] = (natural in new Image()) ?
function () {
return this[0][natural];
} :
function () {
var
node = this[0],
img,
value;
if (node.tagName.toLowerCase() === 'img') {
img = new Image();
img.src = node.src,
value = img[prop];
}
return value;
};
}('natural' + prop, prop.toLowerCase()));
}
For clarity, this is a function definition for those two functions.
HTMLImageElementhere. And if you go check what properties that provides, you should find everything that you need to create the equivalent version of your three examples above using native code..attr("data-src"), that is not image specific. How to access custom data attributes using native JS - developer.mozilla.org/en-US/docs/Learn/HTML/Howto/…img_elem = $('#lv_image');gives you a jQuery wrapper around a DOM element (assuming there is one withid="lv_image").a = document.getElementById('lv_image');gives you the DOM element directly (again, assuming there is one). That's a good first step. Then you need to look how to do the things jQuery's methods do using the nativ DOM. For instance, instead of.attr("src", value)you'd use.setAttribute("src", value)(or, for that specific one,.src = value;). MDN has excellent DOM reference information here.width()/height()methods would set those in CSS, not directly manipulate the _attributes_of the image. But manipulating element styles in JS is a common topic, easy enough to research.