Skip to main content
added 385 characters in body
Source Link
asdf_enel_hak
  • 7.6k
  • 5
  • 45
  • 89

First Case:

<div>
  <a href="#" onclick="doIt(this)">some job</a>
<div>

i can do:

<script>
  function doIt(caller){
       alert($(caller).closest('div').html());
  }
</script>

Second Case:

<div id="divId">
  ....
</div>

i do

<script>
  alert($("#divId").html());
</script>

how can i apply first case mecanism to second case with out onclick(this)?

more details about question:

First case, as i use of anchor onclick, i can get action source then it's parent.

Second case, on the other hand, there is div block and after script block. they are rendered in the page, consequence there is no click event. i need some thing:

$(this).closest('div').html()

but it doesnt work

First Case:

<div>
  <a href="#" onclick="doIt(this)">some job</a>
<div>

i can do:

<script>
  function doIt(caller){
       alert($(caller).closest('div').html());
  }
</script>

Second Case:

<div id="divId">
  ....
</div>

i do

<script>
  alert($("#divId").html());
</script>

how can i apply first case mecanism to second case with out onclick(this)?

First Case:

<div>
  <a href="#" onclick="doIt(this)">some job</a>
<div>

i can do:

<script>
  function doIt(caller){
       alert($(caller).closest('div').html());
  }
</script>

Second Case:

<div id="divId">
  ....
</div>

i do

<script>
  alert($("#divId").html());
</script>

how can i apply first case mecanism to second case with out onclick(this)?

more details about question:

First case, as i use of anchor onclick, i can get action source then it's parent.

Second case, on the other hand, there is div block and after script block. they are rendered in the page, consequence there is no click event. i need some thing:

$(this).closest('div').html()

but it doesnt work

Source Link
asdf_enel_hak
  • 7.6k
  • 5
  • 45
  • 89

$('.selector').closest('div') for non click element

First Case:

<div>
  <a href="#" onclick="doIt(this)">some job</a>
<div>

i can do:

<script>
  function doIt(caller){
       alert($(caller).closest('div').html());
  }
</script>

Second Case:

<div id="divId">
  ....
</div>

i do

<script>
  alert($("#divId").html());
</script>

how can i apply first case mecanism to second case with out onclick(this)?