Reputation: 81
I know this is a commonly asked question on here but I have done my research on the previously answered ones but can't seem to find my error.
As part of the app that I am making, as the page is loaded the document.ready function is supposed to look through each list item in an unordered list, check to see if the id contains the word 'video', and if it does then carry out some actions.
However, my code keeps getting the same error:
Uncaught SyntaxError: Unexpected identifier
After fiddling with the code for a bit, the error disappears when I take out the :contain part but after checking the jQuery docs I thought I was using it correctly.
Any help you could give me would be massively appreciated:
Code:
$(document).ready(function() {
$('ul.thumbnails li a img').each(function() {
if($(this).attr('id'):contains('video')){
var $parent = $(this).closest('a');
$parent.remove();
}
});
});
PS another instance in my code that uses :contains but also throws up this error
$(".span4 img").click(function(){
if($(this).attr('id'):contains('video')){
var id = $(this).attr('id').replace(/video/, '');
console.log(id);
$(this).addClass('hideicon');
$(this).parent('.span4').prepend('<video id="vid></video>');
$(this).siblings("video").trigger("play");
$("video#vid" + id).bind("ended", function() {
$("#video" + id).removeClass('hideicon');
$("video#vid" + id).remove();
});
}
});
Upvotes: 2
Views: 3015
Reputation: 17171
The error that is thrown is in reference to your use of the colon. You should not be using the colon. JavaScript doesn't use the colon to access any methods or fields as some languages do. You access everything with a dot. @thatidiotguy gives you some solutions to fix your code.
Upvotes: 1
Reputation: 8991
I believe you are misusing the contain selector. If you read the api here:
http://api.jquery.com/contains-selector/
You are supposed to do something like $("div:contains('John')")
which would return all elements that have John contained within them. You should be using normal Javascript string functionality to search for video in the id with either:
1) Regular expressions
2) if($(this).attr('id').indexOf('video') != -1){/*code*/}
Upvotes: 1