David Washburn David Washburn - 3 months ago 10
Javascript Question

JQuery - Unexpected Token Can't Resolve

I have an unexpected token error that I cannot seem to resolve.

$(function () {
$('input[type="redditSearch"]').on('change', function () {
var $clone = $(".redditContainer").eq(0).clone().appendTo("#redditMain");
$clone.attr('id',$clone.attr('id').replace(/\d+$/, function(str) { return parseInt(str) + 1; }) );
$clone.find('[id]').each(function() {
var $th = $(this);
var newID = $th.attr('id').replace(/\d+$/, function(str) { return parseInt(str) + 1; });
$th.attr('id', newID);

})

} //Error appears to be here
loadRedditData($(this).val());
});
});


The function works if I remove the $clone function inside of it.

$(function () {
$('input[type="redditSearch"]').on('change', function () {
var $clone = $(".redditContainer").eq(0).clone().appendTo("#redditMain");
$clone.attr('id',$clone.attr('id').replace(/\d+$/, function(str) { return parseInt(str) + 1; }) );
}
loadRedditData($(this).val());
});
});


I have tried a few variations of this function but I cannot seem to get it to run without the error. Any help would be greatly appreciated!

Answer

you need to add an ')' and remove '}' in the next line

$(function () {
    $('input[type="redditSearch"]').on('change', function () {
        var $clone = $(".redditContainer").eq(0).clone().appendTo("#redditMain");
        $clone.attr('id',$clone.attr('id').replace(/\d+$/, function(str) { return parseInt(str) + 1; }) );
        $clone.find('[id]').each(function() {
            var $th = $(this);
            var newID = $th.attr('id').replace(/\d+$/, function(str) { return parseInt(str) + 1; });
            $th.attr('id', newID);

        }) //add ) here

        loadRedditData($(this).val());
    });
});
Comments