0

I am trying to pass arguments to onclick event of dynamically generated element. I have already seen the existing stackoveflow questions but it didn't answer my specific need.In this existing question , they are trying to access data using $(this).text(); but I can't use this in my example.

Click event doesn't work on dynamically generated elements

In below code snippet, I am trying to pass program and macroVal to onclick event but it doesn't work.

onClickTest = function(text, type) {
        if(text != ""){
        // The HTML that will be returned
        var program = this.buffer.program;
        var out = "<span class=\"";
        out += type + " consolas-text";
        if (type === "macro" && program) {
            var macroVal = text.substring(1, text.length-1);
            out += " macro1 program='" + program + "' macroVal='" + macroVal +  "'";
        }
        out += "\">";
        out += text;
        out += "</span>";
        console.log("out " + out);


        $("p").on("click" , "span.macro1" , function(e)
        {
            BqlUtil.myFunction(program, macroVal);
        });

    }else{
        var out = text;
    }
    return out;

};

console.log of out give me this

<span class="macro consolas-text macro1 program='test1' macroVal='test2'">{TEST}</span>

I have tried both this.program and program but it doesn't work.

1 Answer 1

1

Obtain values of span element attributes, since you include them in html:

$("p").on("click" , "span.macro" , function(e)
{
  BqlUtil.myFunction(this.getAttribute("program"), 
    this.getAttribute("macroVal"));
});

There are, however, several things wrong in your code.

  • you specify class attribute twice in html assigned to out,

  • single quotes you use are not correct (use ', not ),

  • quotes of attribute values are messed up: consistently use either single or double quotes for attribute values

var out = "<span class='";

...

out += "' class='macro' program='" + program + "' macroVal='" + macroVal + ;

...

out += "'>";

  • depending on how many times you plan to call onClickTest, you may end up with multiple click event handlers for p span.macro.
Sign up to request clarification or add additional context in comments.

6 Comments

I have move the even handler code outside onClickTest. I realized later that if I put it inside the function , my function wasn't getting triggered at all and only onclickevent was getting triggered. can you give me a sample code snippet of how to pass argument ?
@user911 - Yes, I added it to my answer. You are producing incorrect html. Call console.log(out); before return out; and carefully inspect the result.
Your answer helped me fix my html quoting issues but I am still not able to pass the argument. I have upvoted your answer but still can't accept it. I have tried both this.program and program. It doesn't work. I have also updated my question to fix the quoting issue , please check.
@user911 where in my answer do you see the use of this.program or program? The console output, that you added to your question - is this what you expect? A single class attribute of span tag?
I am doing this inside my onclick function and this works . $(this)[0].attributes.getNamedItem('program').nodeValue) . Your answer helped me to a lot of extent. If you can modify it and add this code snippet, I would be more than happy to accept it.
|

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.