I would like to add a path on an svg, from an "object".
myPath() for the attribute d works fine, but
I want to trigger my function from the object obj, and the obj.action() for d doesn't work... Why ?
var obj = {
action: function() {
myPath();
}
}
function myPath() {
return "M 10 10 L 50 10 L 50 90 L 10 90 Z";
}
$("button").on('click', function() {
// doesn't work... :-(
$("svg").find("path").attr("d", obj.action());
// ... but it's working with this :
//$("svg").find("path").attr("d", myPath());
});
svg {
width: 100px;
height: 100px;
background-color: #C0C0C0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Click Me</button>
<svg><path /></svg>
CodePudding user response:
Resolved :
var obj = {
action : function() { return myPath() }
// action : myPath //other solution
// action : () => myPath() //other solution
}
function myPath() {
return "M 10 10 L 50 10 L 50 90 L 10 90 Z";
}
$("button").on('click', function() {
$("svg").find("path").attr("d", obj.action());
});
