0

I'm trying to run .ajax and insert a data element from the onClick of an item from the page. Whats the best way to do this?

Something like this:

function grabinfo(foo){

      $.ajax({
      url: "infospitter.php",
      method: "GET",
      data: "id="+foo,
      success: function(html){
       $(#showstuff).html(html);
       }
      });

}

  <input onClick="javascript:grabinfo(18343)" />
// and on page each item will have this button input

2 Answers 2

2

It's usually best to keep your javascript unobtrusive, and out of the DOM.

<input type="button" value="18434"/>

In your javascript file:

$('input[type=button]').click(function(){
    $.ajax({
        url: 'infospitter.php',
        method: 'GET',
        data: 'id=' + $(this).attr('value'),
        success: function(returnData){
            // Do something with returnData
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Remove the "javascript:" from your onclick attribute. That's only needed when running code outside a javascript event attribute.

1 Comment

That's not an issue. JavaScript supports block labeling so it will run fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.