Arihant Arihant - 5 months ago 21
jQuery Question

Jquery get width of an element on click

I am using the following jquery code to get the width of an SVG element when clicked on it and write it to the console. I get nothing on the console but no error as well. I am thinking this is because I am not passing the id correctly to the jquery selector. Not really sure though. Below is the code:

$("svg").one("click", function() {
if (this.id != 'main_svg') {
a1 = this.id;
console.log($(a1).width());
$('svg').unbind('click');
console.log(a1);
getdiv2();
}
});

Answer
$("svg").one("click", function() {
  if (this.id != 'main_svg') {
    a1 = this.id;
    console.log($("#" + a1).attr("width"));
    $('svg').unbind('click');
    console.log(a1);
  }
});
Comments