0

I'm new to js and I have this problem


I want on click to change a height of one element to width of another element and this code works properly by for some reason I have to click twice, any suggestions how to make it to work on first click?

<script>
var l = document.getElementById("tablinks");

l.onclick = function(){

var w = document.getElementById("img1").offsetWidth;
document.getElementById("beforeafter1").style.height = w + "px";

};

</script>
Aleksa999
  • 11
  • 1

2 Answers2

0

Try below script.

<script>
//var l = document.getElementById("tablinks");
function doMyTrick(){
    var w = document.getElementById("img1").offsetWidth;
    document.getElementById("beforeafter1").style.height = w + "px";

};
</script>
<input type="button" onclick="javascript:doMyTrick()" value="Submit"/>
Karthick
  • 324
  • 3
  • 12
  • It worked well in one click since it's a simple script. I already tested that script in Chrome browser. Try with some different browsers to verify. – Karthick Apr 26 '20 at 05:47
-1

The link/button code is missing, but I guess you didn't put return false; to your javascript code:

<a href='#' id="tablinks" onclick='someFunc(3.1415926); return false;'>Click here !</a>
Leon
  • 554
  • 4
  • 18
  • Why the downvote? It's a legit answer according to stack overflow: https://stackoverflow.com/a/855376/3520059 – Leon Apr 25 '20 at 19:23