Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript pass function to onclick event

I have to write a function which will create a HTML code of a tag and this function will also pass a function handler to onclick event of created a tag. Code:

function a(text,functionHandler){
    return '<a href="#" onclick="'+functionHandler+'">'+text+'</a>';
}

//usage
var a1 = a('link',function(){
   alert('test');
});

Passing the function to string doesn't work. So my question is: how to pass a function handle to onclick event and get a HTML code of created a tag?

like image 794
David Avatar asked Aug 29 '13 12:08

David


People also ask

How do you pass a function to onClick event?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

How do you pass parameters to onClick react?

In order to pass a value as a parameter through the onClick handler we pass in an arrow function which returns a call to the sayHello function. In our example, that argument is a string: 'James': ... return ( <button onClick={() => sayHello('James')}>Greet</button> ); ...

How do you pass dynamic values on onClick HTML?

For passing multiple parameters you can cast the string by concatenating it with the ASCII value like, for single quotes we can use &#39; var str= "&#39;"+ str+ "&#39;"; the same parameter you can pass to the onclick(str) event.It is helpful in cases where we pass multiple parameters.It works with every browser.

How do you pass a button as this in JavaScript?

If use jquery, access to the element clicked on is simply $(this) . For example: $( ". button" ). on( "click", function() { alert("I am a button with value " + $(this).


2 Answers

Your current approach has a couple of problems -

  1. function is a keyword. You cannot use it as a parameter.
  2. The way you are using it, you need function name here. Passing a callback won't help.

Rather than returning your anchor as a string, return it as an element -

function createAnchor(text, clickHandler) {
    var link = document.createElement('a');    // creates an anchor element
    link.href = "#";                           // sets the href
    link.onclick = clickHandler;               // sets the click handler
    link.innerHTML = text;                     // assigns the inner html string

    return link;                               // now return it
}

and then -

var myAnchor = createAnchor('link', function () {
    alert('test');
});

// Add this element wherever you want

See the documentation on createElement and element.

EDIT

If you want to add this anchor element to an existing DOM element as a child, use the appendChild -

var myAnchor = createAnchor('link', function () {
    alert('test');
});

document.getElementById('x').appendChild(myAnchor);

Live JSFiddle.

EDIT 2

If you really want your anchor string, then create the anchor element using the above function, append it as a child to your desired element, and then use innerHTML on the parent. The approach is demonstrated here -

function createAnchorString(parentId, text, clickHandler) {
    var link,
        parent;

    link = createAnchor(text, clickHandler);    // call the previous function
    parent = document.getElementById(parentId);
    parent.appendChild(link);

    return parent.innerHTML;
}

var myAnchorString = createAnchorString('x', 'link', function () {
    alert('test');
});

alert(myAnchorString);

Live Fiddle.

like image 195
MD Sayem Ahmed Avatar answered Oct 03 '22 22:10

MD Sayem Ahmed


You could try like this :

function a(text, callback)
{
    var obj = document.createElement("a");

    obj.textContent = text;
    obj.onClick = callback;
    return obj.outerHTML;
}

The question is : How will it handle the anonymous function passed as callback argument ?

EDIT This should work now :

function a(text, callback){
    return '<a href="#" onclick="if(!this.fireFunc)this.fireFunc='
            + callback.toString()
            + ';this.fireFunc();">'+text+'</a>';
}

Just using toString() on an anonymous function permits you to get its code.

This is a very hackish way, and doesn't work on named functions (It wouldn't be hard now to make it work).

FIDDLE http://jsfiddle.net/gZ3YD/1/

like image 26
kube Avatar answered Oct 03 '22 20:10

kube