2

I am creating buttons dynamically using jQuery:

function createRefreshButton() {
  var $btn = $('<button/>', {
    text: 'Refresh Data',
    id: 'btn_refresh'
  });

  $($btn).on('click', function () {
    ClickRefresh()
  });

  return $btn;
}

I was wondering if it is possible to change this code to something like this:

function createRefreshButton() {
  var $btn = $('<button/>', {
    text: 'Refresh Data',
    id: 'btn_refresh',
    onclick: 'ClickRefresh()'
  });
  return $btn;
}

However nothing happens when I click the button with that syntax. Is that due to errors in my suggested code or is it simply impossible? Thank you in advance.

4 Answers 4

4

You can set the click property of the object, not onclick, and provide it the reference to the ClickRefresh function. Try this:

function createRefreshButton() {
    return $('<button/>', {
        text: 'Refresh Data',
        id: 'btn_refresh',
        click: ClickRefresh
    });
}

Working example

Sign up to request clarification or add additional context in comments.

2 Comments

I think my problem was that I always tried to add the method name within quotes. This works nicely and is the syntax I was looking for. Thanks!
No problem, glad to help.
2

Normally I do like this :

function createRefreshButton() {
  // Create button
  var $button = $('<button class="btn_refresh">Refresh Data</button>');

  // Append it
  $('body').append($button);

  // Event for this button 
  $button.on('click',function(){
    alert('Refresh');
  });
}

createRefreshButton();
createRefreshButton();

Do not use id if not just only one


PS : If you have same function for those buttons so :

$(document).on('click','.btn_refresh',function(){
   alert('Refresh');
});

.. outside createRefreshButton()


Demo : https://jsfiddle.net/l2aelba/xqj9ycLk/1/

1 Comment

Thanks for suggestion. I had it working, but I was looking to simplify the syntax a bit, like Rory showed in his answer. The snippit I added was for a refresh button. I have only one of those in my code, so no problems with id.
0

It is possible, but you need to change it to the way, jQuery likes:

function createRefreshButton() {
    var $btn = $('<button/>', {
        type: 'button',
        text: 'Refresh Data',
        id: 'btn_refresh'
    }).click(ClickRefresh);
    return $btn;
}

And you should insert the returned value by using either .append() or similar. And it works perfectly fine here:

$(function () {
  function ClickRefresh() {
    console.log("Clicked Refresh");
    $("body").append("<br>Clicked Refresh");
  }
  function createRefreshButton() {
    var $btn = $('<button />', {
      type: 'button',
      text: 'Refresh Data',
      id: 'btn_refresh'
    }).click(ClickRefresh);
    return $btn;
  }
  $("body").append(createRefreshButton());
});
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>

3 Comments

Thanks for suggestion. I was looking to simplify the syntax a bit, like Rory showed in his answer with the click method call inside the var declaration.
@Mr.Blonde There's no difference, that's the same thing even I showed, but I answered before Rory. :) You can be fair and accept whichever is right. :)
My first syntax snippit is working fine, so my question was if it was possible to have a syntax with the method call inside the var declaration.
0

Just use a delegated event handler attached to a non-changing ancestor. This will work on dynamically added content as the selector is run at event time (not event-registration time)

    $(document).on('click', 'button', ClickRefresh);

The add button becomes just this:

function createRefreshButton()
{
    var $btn = $('<button/>', {
        text: 'Refresh Data',
        id: 'btn_refresh'
    });
    return $btn;
}

Note: ID's need to be unique on a page, so if you want to support multiples, you should distinguish these buttons from any other buttons on the page e.g. using a class:

$(document).on('click', '.mybutton', ClickRefresh);

function createRefreshButton()
{
    var $btn = $('<button/>', {
        'class': 'mybutton',
        text: 'Refresh Data',
    });
    return $btn;
}

JSFiddle: https://jsfiddle.net/ubmo441a/

2 Comments

Thanks for suggestion. I had it working, but I was looking to simplify the syntax a bit, like Rory showed in his answer.
@Mr.Blonde: Using inline click handlers defeats some of the event features of jQuery and should be avoided. When using jQuery always use jQuery handlers, but it's your funeral :)

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.