2

I'm trying to dynamically hide specific photos on a page, through Javascript, by selecting their data-id attribute and hiding the photo. This is being achieved through Ajax grabbing the IDs from a TXT file, splitting them in to an Array, and then using jQuery to hide the img with that ID. Note that this function is being passed through a setInterval every 3 seconds...

    function getBlockedIDs() {
            var stringData = $.ajax({
                    url: "http://s61892.gridserver.com/zone/twitter2/blocked.txt",
                    async: false
            }).responseText;
            var blockedArray = new Array();
            blockedArray = stringData.split(",");
            var length = stringData.length
            for (var i = 0; i < length; i++) {
                    $('img.tweetphoto[data-id="' + stringData[i] + '"]').hide();
            }
    }

My problem is, it's not working! No errors are thrown from the console. What's wrong with my code? The idea behind this is to block specific (inappropriate) photos without reloading the page.

Any help is appreciated!

2
  • 1
    Have you used Firebug (or another debugger) to see what's going on? That should be step 1. Commented Nov 14, 2012 at 6:55
  • you can try to use the attribute selector without quotes, e.g: tim.tweetphoto[data-id=foo] Commented Nov 14, 2012 at 7:09

2 Answers 2

1

Are you sure you want to use stringData inside the for loop and not blockedArray? If so change the assignment to length also.

My version would look something like :

     function getBlockedIDs() {
        var blockedArray = $.ajax({
                url: "http://s61892.gridserver.com/zone/twitter2/blocked.txt",
                async: false
        }).responseText.split (/\s*,\s*/);
        for (var i = blockedArray.length; i--;) {
                $('img.tweetphoto[data-id="' + blockedArray[i] + '"]').hide();
        }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would do something like:

function getBlockedIDs() {
    $.get("http://s61892.gridserver.com/zone/twitter2/blocked.txt", function(stringData) {
        var blockedArray = stringData.split(/\s*,\s*/);
        for (var i = 0; i < blockedArray.length; i++) {
            $('img.tweetphoto[data-id="' + blockedArray[i] + '"]').hide();
        }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.