Vinod HC
Vinod HC

Reputation: 1627

JavaScript passing dynamic value in class issue in IE 7 only

I am facing an issue in IE7 only while passing an a dynamic value to javascript. as shown below. this work in all browsers except IE 7.

$val =$id.$i;
echo "<a href='javascript:void(0);' id='network' class=$val value='$octet'>+".$octet.".0.0.0</a> <br />"; 

I am capturing this value in javasript as show below

$(".msg_body #network").click(function(){
    var network = $(this).attr('value');
    alert(network); // alert fails in IE 7

    var clsName = $(this).attr("class");
    alert(clsName); // alert fails in IE 7

complete php code below

<?php 
                $octets = $this->Ip;
                $octetets = $this->octent1;
                $i= 1;
                $id='network';
                foreach($octetets as $octet){
                    $val =$id.$i;

                    echo "<a href='javascript:void(0);' id='network' class=$val value='$octet'>+".$octet.".0.0.0</a> <br />";
                    value='$octet'>+".$octet.".0.0.0</span> <br />";
                    $i++;

                }

            ?>

Upvotes: 0

Views: 128

Answers (2)

SReject
SReject

Reputation: 3936

element Id's must be unique, so instead of using that attribute, use another like name:

Alter your php:

<?php 
            $octets = $this->Ip;
            $octetets = $this->octent1;
            $i= 1;
            $id='network';
            foreach($octetets as $octet){
                $val =$id.$i;

                echo "<a href='javascript:void(0);' name='network' class=$val value='$octet'>+".$octet.".0.0.0</a> <br />";
                value='$octet'>+".$octet.".0.0.0</span> <br />";
                $i++;

            }
        ?>

and your jQuery:

$(".msg_body").on('click', '@network', function(e){
    e.preventDefault();
    var network = $(this).attr('value');
    alert(network); // alert fails in IE 7

    var clsName = $(this).attr("class");
    alert(clsName); // a
});

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

Try this:

$(".msg_body").on('click', '#network', function(e){
    e.preventDefault();
    var network = $(this).attr('value');
    alert(network); // alert fails in IE 7

    var clsName = $(this).attr("class");
    alert(clsName); // a
});

Upvotes: 1

Related Questions