troubleshooting Question

jQuery: passing variables from AJAX generated html into javscript function

Avatar of trevor1940
trevor1940 asked on
HTMLAJAX
13 Comments3 Solutions170 ViewsLast Modified:
Hi
I've been struggling to get this to work and have attempted several solutions none work

Workflow:
User clicks a URL which calls a PHP script, this loads into a div below the link
Within the new content is a button which has attributes these need passing into a new function which again loads new content in this Div  

I tried passing 2 variables within the onclick like here  This caused ReferenceError: MyWord is not defined or SyntaxError: missing ) after argument list if using My Word  

I'm now getting
"id {object HTMLButtonElement) Tag undefined

$(function(){
$('a').click(function(e) {
    e.preventDefault();
    $(this).next('.resp').load( $(this).attr('href') );
});

function myFunction(me){
  var ID = me.getAttribute("id");
  var Tag = me.getAttribute("tag");
  
 $(this).closest(".resp").html ("<p>ID " + ID + "Tag" + Tag + "</p>>");

}

  });

HTML

    <div>
      <a id="1" href="myfile.php?param=My Word&ID=1">My Word</a>
      <div id='resp_1' class='resp'></div>
      <a id="2" href="myfile.php?param=MyWord&ID=2">MyWord</a>
      <div id='resp_2' class='resp'></div>
      
    </div>

HTML generated from first load function

<button id="Butt_1" data-tag="My Word"  onclick="myFunction(this)">My Word</button>
<!-- or second URL -->  
<button id="Butt_2" data-tag="MyWord"  onclick="myFunction(this)">MyWord</button>
SOLUTION
Join our community to see this answer!
Unlock 3 Answers and 13 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros