Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Javascript fire function from object

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>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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());
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!