Al Hennessey
Al Hennessey

Reputation: 2445

GetElementById setting to null instead of int

i have this bit of code that just gets an id and assigns it to a variable for some reason notification_id is set to null when it should be an integer from a php for loop

javascript

$(".pro_content_notification_remove").live('click', function (e) {

 var notification_id = document.getElementById(this);

 e.preventDefault(); 
 $(this).parent().remove();

});

php/html

<?php 
    foreach($notification_array as $key => $value){



       echo '<div class="pro_content_notification_ele" id="'.$value.'">
                                    <div class="pro_content_notification_image '.$notification_image_id.'"></div><!--end pro_content_notifications_image-->
                                    <span class="pro_content_notification_text '.$viewed.'">'.$notification_text.'</span>
                                    <div class="pro_content_notification_remove"></div><!--end pro_content_notification_remove-->
                                </div><!--end pro_content_notification_ele-->';
                    }

    ?>

I have checked the array and page source and the $value being set in the id of pro_content_notification_ele is definately 1, so why is null being set.

Thanks for the help

Upvotes: 0

Views: 59

Answers (3)

bytestorm
bytestorm

Reputation: 1541

Insetead of using

document.getElementById(this)

use

$(this).attr('id')

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

Try using,

var notification_id = e.target.id;

or

var notification_id = $(this).attr('id');

or

var notification_id = this.id;

instead of,

var notification_id = document.getElementById(this);

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388416

this refers to the dom element inside the event handler, so use this.id to get the element's ID.

$(".pro_content_notification_remove").live('click', function (e) {
    var notification_id = this.id;

    e.preventDefault(); 
    $(this).parent().remove();
});

Note: If you are using jQuery >= 1.7, use .on() instead of .live()

Upvotes: 1

Related Questions