Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HTML element from event in jquery?

In a jQuery function I am getting event on form's element click event. Now I want to get its html. How it is possible ?

For Example:

function(event, ID, fileObj, response, data) {
    alert( $(event.target) );            // output: [object Object]
    alert( event.target );               // output: [object HTMLInputElement]
    alert( $(event.target).html() );     // output: (nothing)    
}

I want to get form object who's element is clicked through event

Thanks

like image 507
Student Avatar asked Dec 09 '22 05:12

Student


2 Answers

You can try event.target.outerHTML, however I'm not sure how well supported it is across all browsers. A more convoluted but sure to work solution would be to wrap the element in another element, then take the html of the parent element:

$('<div/>').html($(event.target).clone()).html();
like image 139
Jack Avatar answered Dec 27 '22 04:12

Jack


If your event is a click you can bind it using the click() method from jQuery. Use this to reach your element, like the code below:

$('#id').click(function(event){
  console.log($(this));
});
like image 44
brolim Avatar answered Dec 27 '22 04:12

brolim