How can I select the link in this code using JS DOM?

wilder31

Regular Member
Joined
Oct 18, 2019
Messages
317
Reaction score
77
hello dear members,
JS newbie here needs some help.

The HTML is like this

<tbody><tr>
<td align="center"><input type="image" src="http://thetargetlink.c om " border="0" accesskey="s"></td>
</tr>
</tbody>

I wanna select the link only using JS but can't figure it out.
there's no ID nor CLASS..

how can I select the link?

Can I use document.querySelector?

please help me out!
will be so appreciate
 
Code:
document.querySelectorAll(('table tbody tr:first-child td:first-child input[type=image]'));
Not tested, it should find all inputs of type image, if there are more, you might need to tweak it a bit.
 
Code:
document.querySelectorAll(('table tbody tr:first-child td:first-child input[type=image]'));
Not tested, it should find all inputs of type image, if there are more, you might need to tweak it a bit.
will try! appreciate
 
Code:
// Select the input element with type="image"
const imageInput = document.querySelector('input[type="image"]');

// Access the 'src' attribute
const imageSrc = imageInput.getAttribute('src');

console.log(imageSrc); // Outputs: "http://thetargetlink.com"


Yes, you can absolutely use document.querySelector to select the image and its src attribute, even without an id or class. Since there's no specific identifier like id or class, you can select it based on its tag and attribute values.
 
Use document.querySelector('input[type="image"]').src — it will give you the link directly.
 
Back
Top