4 Replies - 1066 Views - Last Post: 17 August 2012 - 01:33 PM

#1 clatcho  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 14-February 07

retrieving div id from dynamic data

Posted 17 August 2012 - 12:11 PM

hi,

I have dynamic data being displayed via php .

	echo "<h3><div id=\"m_heading\" class=\"muppets\" >";
	echo $value[title];
	echo "</div></h3>"; 



The above is inside a foreach loop

And here is my JQuery code

  <script type="text/javascript"> 
  $(document).ready(function()
  {
    $(this).click(function() {
      var currentId = $(this).attr('id');
      alert(currentId);
	});
});
</script>



The error i get back is :

undefined


I tried

getElementById(this.id)


but that produced

null


i also stripped it back a bit and tried

alert(tagName)


that also produced

undefined


If anyone can throw me a bone, that would be great, or show me a more efficient way that would be very much appreciated

A

Is This A Good Question/Topic? 0
  • +

Replies To: retrieving div id from dynamic data

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3517
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: retrieving div id from dynamic data

Posted 17 August 2012 - 12:27 PM

it depends on where you click, as the handler is tied to the document.

PS. since you suppress PHP notices, you haven’t noticed that string array keys must be properly quoted.
Was This Post Helpful? 0
  • +
  • -

#3 clatcho  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 14-February 07

Re: retrieving div id from dynamic data

Posted 17 August 2012 - 01:01 PM

Yes it would make a difference where we were clicking,

however for the sake of this question, can we pretend that we would be clicking directly on the DIV that we want the ID of
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3517
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: retrieving div id from dynamic data

Posted 17 August 2012 - 01:11 PM

then what do you get when you do alert(this); ?
Was This Post Helpful? 0
  • +
  • -

#5 clatcho  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 14-February 07

Re: retrieving div id from dynamic data

Posted 17 August 2012 - 01:33 PM

[object HTMLDocument]


is returned from

alert(this)


However, I have managed to solve this issue with the following code.

<script type="text/javascript">
  $(document).ready(function(){
    $('.flip').bind('click', function(){ // The div has to have the class flip
      alert($(this).attr('id'));
    });
});
</script>


Thank you for your help.

A
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1