1

I know this is really basic javascript but I'm really not so familiar with javascript.

What I'm trying here is to add prettyPhoto arguments where I want to be. First I get href attribute from link, then I convert it to string, then I take last 4 letters to check is it link to image or to some HTML page. And this code works fine but still my Firebug sends me an error:

TypeError: $hrefy is undefined

txt = $hrefy.toString(); 

How script can work if $hrefy is not defined and how to define it well. This error blocks only javascript code for filtering my portfolio, while other js work fine.

$(document).ready(function(){
$("a[data-rel^='prettyPhoto']").prettyPhoto();

$hrefy = $("article a").has('img').attr("href");
txt = $hrefy.toString();
var lastChar = txt.substr(txt.length - 4);
if (lastChar=='.jpg') {
    $('article a').has('img').attr('data-rel', 'prettyPhoto'); 
}

$('a img').click(function () {  
    var desc = $(this).attr('title');  
    $('a').has('img').attr('title', desc);  
});  

});

8
  • 4
    It already is a string. Commented Mar 25, 2013 at 16:55
  • 1
    Why are $hrefy and txt global variables? Do they need to be? Add var before them if they don't. Commented Mar 25, 2013 at 16:59
  • Perhaps you meant $hrefy = $("#article a > img").parent().attr("href");? Commented Mar 25, 2013 at 17:02
  • You can check page where problem is valeka.net/karen/page-a Commented Mar 25, 2013 at 17:06
  • You are trying to retrieve the href property from the img elements, but the href is not a valid property for that element. You use href in the a tag to create hyperlinks. Don't you want to retrieve the a elements? Commented Mar 25, 2013 at 17:07

1 Answer 1

1

After looking into the source of the page you've linked, I've noticed that there is no <article> element declared anywhere. So, your jquery selector does not return anything and attr('href') is undefined.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I've couldnt ever solve this by myself! Thanks again!

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.