How to get parent object ID when clicking on ellipse element? - javascript
If I'm interpreting correct hierarchy of elements, what I'm wanting to do would be ...
That element <ellipse> children of <svg>, <object> and <div id="svg_*"> when clicked showed me the id of your <object> parent.
this * is because the <ellipse> will be from your respective svg div.
This is how my HTML strtucture is, thoses #DOCUMENT are window.frames opened by each <object> tag.
<body>
<div id="content">
<div class="header"></div>
<nav class="nav"></nav>
<div class="div 1"></div>
<div class="div 2">
<div id="svg_1">
<object id="OneNameObject" data="../folder/fileOne.svg">
#DOCUMENT
<svg>
<ellipse>I have one addEventerLinster Click here</ellipse>
</svg>
</object>
</div>
<div id="svg_2">
<object id="TwoNameObject" data="../folder/fileTwo.svg">
#DOCUMENT
<svg>
<ellipse>I have one addEventerLinster Click here</ellipse>
</svg>
</object>
</div>
</div>
<div class="div 3"></div>
<div class="div 4"></div>
</div>
For now I'm getting access to the <object> id when I specify the index of it to be used.
0 for svg_1 #document
1 for svg_2 #document
So I thought I'd use the this selector to call the ellipse hireararchy. But I could not, and i don't know how.
something like :
this.parent.document.querySelector('object').id;
This is the way i getting the id using index, and by this - for sure - i aways get the id from selected <object>.
window.onload = function (){
//This is for addEventLinster on each <ellipse>.
var obj = document.querySelectorAll('object');
for (var x = 0; x < obj.length; x++){
var svg = obj[x].contentDocument.querySelector('svg');
var e = svg.querySelectorAll('ellipse');
for ( var i = 0; i < e.length; i++){
e[i].addEventListener('click', function(){
var objId = parent.document.getElementsByTagName('object')[0].id;
alert( objId );
});
}
}
};
Working Example \ but not functional on my context.
This code is working, but it's not on my context ! Need to get that info, I am using <object> data attr to load svg files, they come on embed tags with a new window frame and a new document page. That is why i cant get they elements so easy.
Image HTML Code / DOM painel - * detail : ellipses is on <g> tag.
$(document).ready(function(){
var obj = $('ellipse');
obj.on('click', function(){
alert($(this).parents('object').attr('id'));
});
});
div {
width: 52px;
margin: 2px;
border-radius: 10%;
}
#svg_1:hover, #svg_2:hover {
opacity: 0.7;
}
#svg_1 {
border: 2px solid indianred;
}
#svg_2 {
border: 2px solid lightblue;
}
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="content">
<div class="header"></div>
<nav class="nav"></nav>
<div class="div 1"></div>
<div class="div 2">
<div id="svg_1">
<object id="redObject" data="">
<!-- #DOCUMENT -->
<svg width="50" height="48" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<ellipse ry="14" rx="13" id="svg_2" cy="25" cx="25" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#cd5c5c" fill="#bf2a2a"/>
</svg>
</object>
</div>
<div id="svg_2">
<object id="blueObject" data="">
<!-- #DOCUMENT -->
<svg width="50" height="50" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
<ellipse ry="14" rx="13" id="svg_2" cy="25" cx="25" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#c95c5c" fill="#56aaff"/>
</svg>
</object>
</div>
</div>
<div class="div 3"></div>
<div class="div 4"></div>
</div>
</body>
</html>
ps; I found the title of the question a bit confusing, in case someone has a better suggestion, I would appreciate it!
Give the below a try. (Not a tested code but should ideally work unless some syntax issues or so)
We can check if something can unnecessary can be removed.
$.each($('[id^="svg_"]'),function(){
var eclipse = $(this).find("object").find('svg').find('ellipse');
console.log(eclipse)
$(eclipse).click(function (){
alert($(this).parent().parent().attr('id'))
})
});
Related
How to click svg using querySelector?
I am trying to build a chrome extension to click an SVG on a webpage using the below command but it shows that "click" is not a function. Can anyone give me some direction in order to solve this issue? document.querySelector("div[class^='SubmitChat__SubmitButton']").querySelector("svg > path(0)").click(); <form class="SubmitChat__SubmitChatWrapper-kLTVjd ezNulO"> <div id="PinToTop" style="display: none; border: groove; margin: 5px; padding: 5px;"></div> <div class="DefaultComments__DefaultCommentsWrapper-drohEF dbvkCO"> <div class="SubmitChat__TextAreaWrapperBox-ijaLYA uQa-DJ"> <textarea data-track-category="Interaction_Comment" data-track-action="click" data-track-name="field_comment" class="SubmitChat__TextAreaWrapper-cEDMAF joGtiU" style="height: 44px;"></textarea> </div> <div class="SubmitChat__SubmitButton-kXYuum kSQpDq"> <span class="isvg loaded SubmitChat__SendMessageIcon-hzVurU lcJXRC"> <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"> <path d="M25.305 16.07L7.503 24.307a.802.802 0 01-1.111-.925l1.923-7.493h0L6.392 8.396a.8.8 0 011.111-.925l17.802 8.236a.2.2 0 010 .363zm-16.638-.181h16" fill="none" stroke="#28232D" stroke-linecap="round" stroke-width="1.5"> </path> </svg> </span> </div> </div> </form>
You can't really click an svg path but here is a solution: const pathElem = document.querySelector("div[class^='SubmitChat__SubmitButton'] svg > path") pathElem.onclick = () => console.log("clicked") pathElem.dispatchEvent(new Event("click")) pathElem.dispatchEvent(new Event("click")) <form class="SubmitChat__SubmitChatWrapper-kLTVjd ezNulO"> <div id="PinToTop" style="display: none; border: groove; margin: 5px; padding: 5px;"></div> <div class="DefaultComments__DefaultCommentsWrapper-drohEF dbvkCO"> <div class="SubmitChat__TextAreaWrapperBox-ijaLYA uQa-DJ"> <textarea data-track-category="Interaction_Comment" data-track-action="click" data-track-name="field_comment" class="SubmitChat__TextAreaWrapper-cEDMAF joGtiU" style="height: 44px;"></textarea> </div> <div class="SubmitChat__SubmitButton-kXYuum kSQpDq"> <span class="isvg loaded SubmitChat__SendMessageIcon-hzVurU lcJXRC"> <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"> <path d="M25.305 16.07L7.503 24.307a.802.802 0 01-1.111-.925l1.923-7.493h0L6.392 8.396a.8.8 0 011.111-.925l17.802 8.236a.2.2 0 010 .363zm-16.638-.181h16" fill="none" stroke="#28232D" stroke-linecap="round" stroke-width="1.5"> </path> </svg> </span> </div> </div> </form> Note that .click() works with HTML but not SVG elements. MDN Also note that the click event handler in your case is probably not listening on the path element but rather on the svg, div or span tag.
how to let the svg image completely move inside the div tag only?
I can move my svg image(animated character) in any direction but I want to restrict the movement inside play area div tag only and also it's not using all the area of the play area div tag to move properly.Here's a gif for the reference <div class="playarea"> <svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="gokusvg" > <image id="goku" height="140" width="150" xlink:href="images/goku.png" transform=""> </image> </image> </svg> .playarea { grid-area: playarea; border:2px solid black; height:400px; width:400px; } /* image svg */ #gokusvg { height:400px; width:400px; } Google Blockly javascript file //block definition for Up arrow block Blockly.Blocks['up'] = { init: function() { this.appendDummyInput() .setAlign(Blockly.ALIGN_CENTRE) .appendField(new Blockly.FieldImage( "images/up.svg", 50, 50,"*",Up)); this.setColour("#FFFFFF"); } }; /*character movement funtions*/ //moves the character in upward/north direction function Up() { // TODO: Assemble JavaScript into code variable. var code = document.getElementById('goku').style.y="-100"; var sound = new Audio("sounds/move.mp3"); // buffers automatically when created sound.loop = false; sound.play(); }
> Add bootstrap class and use raw and column span for stay image into div and give % to playarea class instead of height and width in px.It will help you. <script> src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"> </script> <style> .playarea { grid-area: playarea; border:2px solid black; height:100%; width:100%; } /* image svg */ #gokusvg { height:400px; width:400px; } </style> <div class="raw"> <div class="col-sm-1"></div> <div class="col-sm-10 overflow: hidden;"> <div class="playarea"> <svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="gokusvg" > <image id="goku" height="140" width="150" xlink:href="images/goku.png" transform=""> </svg> </div> </div> <div class="col-sm-1"></div> </div>
How to convert all elements inside html div element into an image using javascript without any third party library
I am having a div with 3 elements. I want to convert that whole div into an image without using third party tool. Kindly provide me any suggestion regarding this. <!DOCTYPE html> <html> <body> <div id="div1">DIV 1<br> <svg width="50" height="50"> <path d="M0,0 L50,0 L50,50 Z" style="stroke: #006666; fill:none;"/> </svg> <div>DIV 2</div> <input type="button" value="Button"/> </div> </body> </html>
This is probably a good starting point. I have slightly modified the answer from this question https://stackoverflow.com/a/27232525/8085668. I have just removed the callback and added a click event. Please refer to it for a detailed explanation of the code. var svgText = document.getElementById("myViewer").outerHTML; var myCanvas = document.getElementById("canvas"); var ctxt = myCanvas.getContext("2d"); var btn = document.getElementsByClassName('convert2Png')[0]; function drawInlineSVG(ctx, rawSVG) { var svg = new Blob([rawSVG], {type:"image/svg+xml;charset=utf-8"}), domURL = self.URL || self.webkitURL || self, url = domURL.createObjectURL(svg), img = new Image; img.onload = function () { ctx.drawImage(this, 0, 0); domURL.revokeObjectURL(url); }; img.src = url; } // usage: btn.addEventListener('click', function() { drawInlineSVG(ctxt, svgText); }); <!DOCTYPE html> <html> <body> <div id="div1">DIV 1<br> <svg width="50" height="50" id="myViewer" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"> <path d="M0,0 L50,0 L50,50 Z" style="stroke: #006666; fill:none;"/> </svg> <div>DIV 2</div> <input class="convert2Png" type="button" value="Button"/> <canvas id="canvas" width=800 height=600 style="margin-top:10px;"></canvas> </div> </body> </html>
Get image height and change the image container height to auto
I'm trying to determine the height of images in containers and if any image has a height of less than 300 pixels then I want its container height to be set to auto. Here is my code: <style> .container { width: 500px; height: 500px; } </style> <script> var img = document.getElementsByTagName("img"); if (img.height < 300) { var x = document.getElementsByClassName('container'); x.style.height="auto"; } </script> <div class="container"> <img height="500" width="500" src="/some_img.jpg"/> </div> <div class="container"> <img height="500" width="500" src="/some_img.jpg"/> </div> <div class="container"> <img height="500" width="500" src="/some_img.jpg"/> </div> <div class="container"> <img height="250" width="250" src="/some_img.jpg"/> // This image height is less than 300 pixels. </div> <div class="container"> <img height="500" width="500" src="/some_img.jpg"/> </div> Any help would be appreciated.
First you get all image tags, then itterate through them, if their height is less than 500, then you get its parent element and set its height to 'auto' here is a working example var img = document.getElementsByTagName("img"); console.log(img) for (index in img){ if (img[index].height < 300) { img[index].parentNode.style.height="auto"; } }
img = getElementsByTagName("img") returns all images, not just one, so you can't use it as img.height, you should use for loop or jQuery selector x = document.getElementsByClassName('container') also returns all elements, so you can't set its style directly. And you need to set style for parent element of current processed image, not just first or all of them. You can use jQuery .parent() Your script is executed before the img elements begin to exist. Either put you script after html or use jQuery $(document).ready() event I think you should put a dot before slash in src (src="./some_img.jpg") So with jQuery it will look like this: <script> $(document).ready(function(){ $("img").each(function(){ if( $(this).height() < 300 ) { $(this).parent().height( "auto" ); } }); }); </script>
Vanilla JS Iterate over the images. If they are <300 and their parent has the class of 'container' then set the parent's style.height to 'auto: var imgs = document.getElementsByTagName("img"); for (i in imgs){ if (imgs[i].height < 300 && imgs[i].parentNode.className.indexOf('container') != -1) { imgs[i].parentNode.style.height="auto"; } } var imgs = document.getElementsByTagName("img"); for (i in imgs){ if (imgs[i].height < 300 && imgs[i].parentNode.className.indexOf('container') != -1) { imgs[i].parentNode.style.height="auto"; } } .container { width: 500px; height: 500px; } <div class="container"> <img height="500" width="500" src="http://www.placehold.it/500/500/"/> </div> <div class="container"> <img height="500" width="500" src="http://www.placehold.it/500/500/"/> </div> <div class="container"> <img height="500" width="500" src="http://www.placehold.it/500/500/" /> </div> <div class="container"> <img height="250" width="250" src="http://www.placehold.it/250/250/"/><!--This image height is less than 300 pixels.--> </div> <div class="container"> <img height="500" width="500" src="http://www.placehold.it/500/500/"/> </div> jQuery Use jQuery's filter() to simply find all .containers that contain images with a height < 300: $(".container") .filter(function(){return $(this).find("img").height() < 300;}) .css("height", "auto"); $(function(){ $(".container").filter(function(){return $(this).find("img").height() < 300;}).css("height", "auto"); }); .container { width: 500px; height: 500px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <img height="500" width="500" src="http://www.placehold.it/500/500/"/> </div> <div class="container"> <img height="500" width="500" src="http://www.placehold.it/500/500/"/> </div> <div class="container"> <img height="500" width="500" src="http://www.placehold.it/500/500/" /> </div> <div class="container"> <img height="250" width="250" src="http://www.placehold.it/250/250/"/><!--This image height is less than 300 pixels.--> </div> <div class="container"> <img height="500" width="500" src="http://www.placehold.it/500/500/"/> </div>
How to highlight the entire map using svg in HTML file?
this is coding for map. If i mouse over the map it highlight only half of the state is displayed. i need to highlight the full state. How to do it? can anyone help me with codings... her is our HTML coding: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <script type="text/javascript" src="js/jquery-1.js"> var aid; var disid; var but; var hovid; </script> <title>Sample Map</title> <style type="text/css"> .button { list-style-type: none; padding: 0; margin: 0; width: 100px; font-family:"Arial", Helvetica, Sans-serif; font-size:14px;v font-weight: bold; } </style> </head> <body> <div> <!-- <p><a><button class="button" onclick="getid('VA','1','VA1','46','')";>Zoom</button></a></p> --> <p><a><button class="button" onclick= "sample.style.zoom='2300%'">Zoom</button></a></p> <button class="button"onclick= "sample.style.zoom='100%'">Zoom Out</button> </div> <!-- <table> <tr> <td> --> <div id="sample"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="700" height="500"> <polyline fill="#c0c3c5" id="KY1" title="Kentucky" alt="Kentucky" stroke="#FFFFFF" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" points=" 500.441,263.861 519.982,261.672 527.197,259.595 527.905,257.709 530.413,256.524 530.479,255.049 532.128,253.862 532.072,252.457 540.856,244.216 540.864,244.264 538.589,244.089 537.851,243.049 536.241,242.449 535.75,240.996 533.173,238.515 533.231,237.692 530.597,234.998 531.316,233.435 530.781,230.627 530.781,230.627 528.975,228.691 527.19,228.215 525.973,225.548 524.385,226.236 523.034,228.147 521.104,228.767 518.115,227.233 516.802,227.828 516.454,228.738 515.22,228.559 513.299,226.783 508.794,226.435 507.269,223.303 505.197,221.858 503.135,222.735 501.486,221.724 499.633,223.385 500.554,224.641 500.064,225.881 501.259,226.137 501.105,227.831 498.844,228.127 496.451,229.948 495.07,229.116 492.96,229.656 493.607,232.788 491.278,234.587 490.614,237.195 488.653,237.714 487.86,239.469 487.837,241.85 486.289,243.296 483.179,241.962 482.883,240.586 481.676,239.776 482.273,240.805 480.57,241.011 480.997,241.997 479.969,242.69 480.156,244.352 479.224,244.76 478.745,245.989 478.408,245.047 477.161,245.259 475.939,243.798 473.506,245.307 472.427,247.555 468.408,245.295 466.396,245.884 465.938,244.891 466.096,246.879 465.344,247.479 464.821,246.169 463.313,246.82 462.113,245.998 461.558,246.416 462.041,247.836 461.383,248.588 460.169,248.138 458.925,250.765 460.069,253.706 454.872,255.64 454.62,257.494 455.961,259.672 454.62,261.216 448.309,258.937 446.057,261.607 446.887,263.396 446.925,266.685 445.605,270.267 443.757,269.407 442.896,271.563 461.841,270.314 461.136,267.126 464.089,267.174 464.391,267.746 481.355,266.352 481.978,265.647 500.441,263.861" onmouseover="getstate('KY','17','','KY1');" onmouseout="onout('KY1');"><title>Kentucky</title></polyline> <polyline fill="#c0c3c5" id="OH1" title="Ohio" alt="Ohio" stroke="#FFFFFF" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" points=" 545.233,168.086 533.923,176.797 530.205,177.313 526.158,179.271 521.791,179.139 521.14,178.462 519.449,178.738 518.338,178.549 516.531,177.317 513.018,176.452 495.778,178.437 500.535,222.528 501.486,221.724 503.135,222.735 505.197,221.858 507.269,223.303 508.794,226.435 513.299,226.783 515.22,228.559 516.454,228.738 516.802,227.828 518.115,227.233 521.104,228.767 523.034,228.147 524.385,226.236 525.973,225.548 527.19,228.215 528.975,228.691 530.781,230.627 534.104,229.693 534.405,227.264 535.615,226.61 534.705,223.909 536.431,219.708 537.908,219.829 538.283,221.624 539.354,220.335 540.222,220.539 539.172,218.129 539.979,217.634 539.759,216.364 540.446,214.741 541.875,214.269 543.063,211.814 544.403,212.763 546.07,211.648 549.936,207.213 550.254,205.473 549.648,204.752 550.342,203.011 550.119,202.119 550.712,201.92 550.564,199.059 551.643,195.083 551.105,193.692 551.299,192.52 550.067,190.643 550.456,189.982 551.832,189.336 551.869,189.331 548.445,167.054" onmouseover="getstate('OH','35','','OH1');" onmouseout="onout('OH1');"><title>Ohio</title></polyline> <polyline fill="#c0c3c5" id="TN1" title="Tennessee" alt="Tennessee" stroke="#FFFFFF" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" points=" 547.005,258.142 519.982,261.672 481.978,265.647 481.355,266.352 464.391,267.746 464.089,267.174 461.136,267.126 461.841,270.314 442.896,271.563 442.566,272.441 442.116,270.633 441.231,270.85 441.993,273.718 440.677,274.601 441.598,275.787 439.899,276.097 441.156,277.86 439.612,280.212 440.607,281.83 439.073,282.184 439.889,282.889 439.577,283.445 436.892,284.996 437.67,286.516 436.927,287.202 437.466,288.361 436.222,288.641 434.894,291.565 435.431,295.197 434.263,295.366 433.888,296.621 432.572,297.281 461.637,295.572 496.773,292.57 514.591,290.681 514.564,287.004 515.268,286.275 517.391,286.089 517.988,283.31 519.572,281.555 521.415,280.522 524.753,280.015 528.858,276.253 531.123,275.589 532.06,273.732 531.916,272.61 533.325,272.756 533.646,271.518 535.344,270.267 536.552,271.847 539.736,268.385 540.869,267.848 542.748,268.568 544.534,264.847 545.677,263.764 547.03,263.848 546.424,262.896 546.94,261.359 546.537,260.654 547.005,258.142" onmouseover="getstate('TN','42','','TN1');" onmouseout="onout('TN1');"><title>Tennessee</title></polyline> </svg> </div> <!--</td> </tr> </table> --> <script type="text/javascript" > function getstate(aid,disid,but,hovid) { var elem = document.getElementById(hovid); elem.setAttributeNS(null,"stroke","#404040"); elem.setAttributeNS(null,"stroke-width","2.35"); } function onout(hovid) { var elem1 = document.getElementById(hovid); elem1.setAttributeNS(null,"stroke","#FFFFFF"); elem1.setAttributeNS(null,"stroke-width","2.35"); } </script> </body>
This has to do with a layering order. Your middle state is buried under the other two. To bring it to top so that the border shows up topmost, you have to insert the element last. A quickfix would be to give your element an ID: <svg id="id_svg" ... Then in your getstate() function, insert this at the end: document.getElementById('id_svg').appendChild( elem ); A possibly better way to handle this set of complexities is to use a library such as raphaeljs. It fixes many annoyances, and is cross-browser compatible. For example, see this fiddle: http://jsfiddle.net/3345Q/