tags:

views:

113

answers:

2

I dont understand what would be the problem with the following code. It needs to copy image's id value to another textbox but instead I get an error.

Unexpected end of file while searching for ']' to end attribute selector.

<script>
$(function() { 
    $(".floatLeft").click(function() {
       var id = $(this).attr("id").replace(/\D/g, "");
       $("input[name='photo[" + id + "]'").val(Math.abs($("input[name='photo[" + id + "]'").val() - 1));
    });
});
</script>

      <ul class="thumbs">

        <li>
                <img src="/FLPM/media/news/images/2M9Y1I2K_sm.jpg" alt="Garden" id="28" class="floatLeft" />
                <input type="text" name="photo28" value="0" />

            <br />
            <a href="?Process=&IMAGEID=28" class="thumb"><span class="floatLeft">DELETE</span></a>
        </li>

        <li>
                <img src="/FLPM/media/news/images/2A9L1V2X_sm.jpg" alt="Frangipani Flowers" id="27" class="floatLeft" />
                <input type="text" name="photo27" value="0" />
            <br />
            <a href="?Process=&IMAGEID=27" class="thumb"><span class="floatLeft">DELETE</span></a>

        </li>
    </ul>
+3  A: 

Your code should be:

$(".floatLeft").click(function() {
   var id = $(this).attr("id").replace(/\D/g, "");
   $("input[name='photo\\[" + id + "\\]']").val(Math.abs($("input[name='photo\\[" + id + "\\]']").val() - 1));
});

All i did was read the error message

Jacob Relkin
I still get the same error.
zurna
That's because you have the same error in the first selector.
Jacob Relkin
A: 
$("input[name='photo[" + id + "]'")

one ] is missing

I'm not sure but you should not wrap attribute value in single quotes. And also double escape brackets in attribute value

$("input[name=photo\\[" + id + "\\]]")
dev-null-dweller