$("a[href*='http://www.google.com']").attr('id','newId');
Can only reference it by href.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, that’s the correct way to add an
idattribute to an element that doesn’t already have one.However you should ensure that there’s only one matching element, as it’s incorrect to have two elements with the same ID on a page, e.g.:
This would, of course, still leave you with the problem of what to do with the remaining matching elements, if any. A possible solution would be:
which will assign the elements the IDs
newId_0,newId_1, etc.