vkv
vkv

Reputation: 1010

find the text inside out side the target element?

I am trying to find the text in the dropped place

this.on("drop", function (event) {
    console.log("event", event)
    var imageUrl = event.dataTransfer.getData('URL');
    console.log(imageUrl)
    console.log($(event.target))
    console.log($(event.target).closest('span'))
    //console.log($(event.target).parent().getElementById('displayName'),"by id")
    console.log($(event.target).parent().getElementsByTagName('span'), "by tag")
});

The html is like

<div class="main-div">
<form class="form"><img src=""/></form>
<div><span>required text</span></div>
<div>

when I do console.log($(event.target)) some time I am getting the form elemnt, some times I am getting the image tag. then how can I find the text which I mentioned as required text?

Upvotes: 0

Views: 82

Answers (1)

Satpal
Satpal

Reputation: 133403

As you event.target refers to FORM/IMG element. Use .closest() to traverse up to form, then use .next() to target immediately following sibling DIV then .find() to get SPAN element

$(event.target).closest('form').next('div').find('span').text()

Upvotes: 1

Related Questions