i have sầu a div for which i set value dynamically during run time, if there is value than i have sầu enable or create a liên kết which will have sầu oncliông xã method where i will Điện thoại tư vấn a javascript method.

how khổng lồ vị this in jquery or javascript?

I set value khổng lồ a div lượt thích the following,

document.getElementById("attachmentName").innerHTML=projectInforamtionMap.Cim_AttachmentNames;this the div :

Bạn đang xem: Has link with a url in onclick attribute




Xem thêm: Con Gái Yêu Bằng Tai - Phụ Nữ Yêu Bằng Tai Là Do Đâu

Assume that your function are previously define like this

function foo()alert("function call");After adding innerHTML

In Javascript

document.getElementById("attachmentName").setAttribute("onclick","foo()");In Jquery




You can bởi this without inserting liên kết in the div in following way

document.getElementById("attachmentName").onClick = function () alert(1); // To chạy thử the cliông chồng ;You can achieve it with the help of jQuery as well in the following way.

$("#attachmentName").click(function () alert(1); );for this you have sầu khổng lồ include jQuery liabray on the page. refer jQuery.com

Still if you forecefully want lớn include the links in Div then following is the code for it

var link = $(""+ $("#attachmentName").text() +"");$("#attachmentName").html($(link);links.click(function () alert(1); );Hope this would help.

You have several alternatives


// var elem = document.getElementById("attachmentName");elem.onclick = function() ;elem.setAttribute("onclick","foo()");elem.addEventListener("onclick", foo, false); // The most appropiate wayjQuery:

// var elem = $("#attachmentName");elem.click(foo);elem.on("click", foo);$("body").on("click", elem, foo);Between the 3 jQuery alternatives, the last is the best one. The reason is due to lớn the fact that you are attaching an sự kiện just the toàn thân element. In this case, it does not matter but in other cases, you are probably willing lớn attach the same sự kiện to a collection of elements, not just one.

Therefore, using this approach, the sự kiện is attached lớn the body toàn thân but works for the elements clicked, instead of attaching the same sự kiện lớn every element, so it"s more efficient :)