Moti Mashiah
asked on
Jquery syntax
Hi guys,
I'm trying to inject element to the dom with jquery html method, and have event handler with this event which get variable.
Here is what I'm doing:
In the example above I'm getting syntax error, but if I take the parameter out and just run the function then it works.
can somebody help and send me the right syntax for this.
thanks,
I'm trying to inject element to the dom with jquery html method, and have event handler with this event which get variable.
Here is what I'm doing:
var ilc = 100;
$("myid").html("<a onclick='myfunction(" + ilc + ")'</a>");
In the example above I'm getting syntax error, but if I take the parameter out and just run the function then it works.
can somebody help and send me the right syntax for this.
thanks,
ASKER
thank you for paying attention.
it was just missed typo when I copy my code, but this is wasn't my question.
here is the full code with the #
var ilc = 100;
$("#myid").html("<a onclick='myfunction(" + ilc + ")'</a>"); // in my case it is id.
Please can you answer my question above.
thank you soo much.
it was just missed typo when I copy my code, but this is wasn't my question.
here is the full code with the #
var ilc = 100;
$("#myid").html("<a onclick='myfunction(" + ilc + ")'</a>"); // in my case it is id.
Please can you answer my question above.
thank you soo much.
Does it work if you hard-code the ilc value?
$("#myid").html("<a onclick='myfunction(100)'</a>"); // in my case it is id.
ASKER
I can try.
let me try and get back to you on it.
let me try and get back to you on it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you that works,
your syntax was correct.
thanks soo much.
your syntax was correct.
thanks soo much.
This is arguably a more correct way of doing it. If you are going to create the element with javascript then might as well attach the click handler directly when you create it.
$("#myid").append(
$('<a/>').click(myfunction(lic))
)
Note tag is empty - so not sure what you will be clicking on.
ASKER
Hi Julian thank you for your answer.
Can I send you my full script I really scratch my head how to pass this string parameter through html jquery method.
Here is my story:
what I'm trying to do is to inject html into dom in this way:
Note: it is just an example:
Now after run this method it is in the dom.
so what I'm trying to do now is to click on "delete" and initiate the method "deleteParent" method and send string to this method:
Note: if I pass number it works like onclick="deleteParent(345)
Please help dear Julian.
I was trying your example but it didn't work for me.
Can I send you my full script I really scratch my head how to pass this string parameter through html jquery method.
Here is my story:
what I'm trying to do is to inject html into dom in this way:
Note: it is just an example:
function EditDeleteOrqtyChange() {
var ilc = "COLE 946016618-RL";
var qty = 20
var itmparentid = 234;
$("#push_edit_parentid").show();
$("#push_edit_parentid").html('<div class="col-md-6 alert alert-info"><div class="row"><div class="col-md-12"><div class="col-md-8"><span>' + ilc + '</span></div>'
+ '<div class="col-md-4"><a onclick="deleteParent('+ ilc + ')">Delete</a></div></div></div>'
+ '<br /><div class="row"><div class="col-md-12"><div class="col-md-8">'
+ '<input type="number" value="' + qty + '"/></div><div class="col-md-4"><a onclick="changeQtyforparent(' + itmparentid + ')"><strong>Change</strong></a>'
+ '</div></div></div></div>')
}
Now after run this method it is in the dom.
so what I'm trying to do now is to click on "delete" and initiate the method "deleteParent" method and send string to this method:
function deleteParent(ilc) {
console.log(ilc)
}
Note: if I pass number it works like onclick="deleteParent(345)
Please help dear Julian.
I was trying your example but it didn't work for me.
ASKER
Hi Julian,
I got it ...so I don't need help.
Thank you anyways.
onclick="deleteParent(\'' + ilc + '\') //apparently this is the way to send string.
I got it ...so I don't need help.
Thank you anyways.
onclick="deleteParent(\'' + ilc + '\') //apparently this is the way to send string.
That will work - my example was trying to illustrate the difference between passing html to .html() method as opposed to creating the elements and setting their attributes.
The html approach may seem easier but it is easy to get lost in the string concatenation and escaped characters (quotes).
Another aspect of the example was to highlight the practice of attaching event handlers to elements rather than implementing them as attributes on the elements (element.click vs onclick)
In cases where complex HTML is required - you could consider using a template that you load with JavaScript and then modify as needed.
Having said that you appear to have a solution - my comments where intended more as background information for you and anyone else who might be reading this thread.
The html approach may seem easier but it is easy to get lost in the string concatenation and escaped characters (quotes).
Another aspect of the example was to highlight the practice of attaching event handlers to elements rather than implementing them as attributes on the elements (element.click vs onclick)
In cases where complex HTML is required - you could consider using a template that you load with JavaScript and then modify as needed.
Having said that you appear to have a solution - my comments where intended more as background information for you and anyone else who might be reading this thread.
ASKER
Dear Julian,
I got another issue with my code(like the same code I post earlier).
Just to remind you this is my code:
What I'm trying to do now is - after the input (id="qtychid") created I have this element with onclick even on it see below:
Now I want to take whatever user put into this input box and send it to another method. the issue I experience now is that the variable comes as undefined.
Please, let me know your idea.
Thanks,
I got another issue with my code(like the same code I post earlier).
Just to remind you this is my code:
$("#push_edit_parentid").html('<div class="col-md-6 alert alert-info"><div class="row"><div class="col-md-12"><div class="col-md-8"><span>' + ilc + '</span></div>'
+ '<div class="col-md-4"><a onclick="deleteParent(\'' + ilc + '\')">Delete</a></div></div></div>'
+ '<br /><div class="row"><div class="col-md-12"><div class="col-md-8">'
+ '<input type="number" id="qtychid"/></div><div class="col-md-4"><a onclick="changeQtyforparent(\'' + $("#qtychid").val() + '\')"><strong>Change</strong></a>'
+ '</div></div></div></div>')
What I'm trying to do now is - after the input (id="qtychid") created I have this element with onclick even on it see below:
onclick="changeQtyforparent(\'' +$("#qtychid").val() + '\')"><strong>Change</strong></a>
Now I want to take whatever user put into this input box and send it to another method. the issue I experience now is that the variable comes as undefined.
Please, let me know your idea.
Thanks,
ASKER
Hi Julian,
I figured it out again LOL...
but thank you again..
anyway all I had to do is to create the variable in the next method to make it readable.
Thanks,
I figured it out again LOL...
but thank you again..
anyway all I had to do is to create the variable in the next method to make it readable.
Thanks,
e.g.
$("#myid").html // for an id
or
$(".myid").html // for a class