Jan Thiemen
Jan Thiemen

Reputation: 103

Undefined variable error that shouldn't be a variable

I've made a piece of code in jquery that assigns a href attribute to a variable. Here's that code:

$('#reactions' + i).attr('href', 'javascript:comments ('+entry.url+','+i+');');

This should assign a call to the javascript function comments. Now I want to use that call on a jquery mobile button, like this:

document.write('<a href="#" id="reactions' + i + '" data-role="button" class="ui-btn-right">Reactions</a>');

But doing this gives me a in FF and Chrome. This is the error from FF±

Uncaught exception: ReferenceError: Undefined variable: i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what Error thrown at line 1, column 0 in javascript:comments (i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what,1);: comments (i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what,1);

In this, i_heart_chaos_ihc_after_dark_independence_day_through_a_bullhornthis_is_what is the value of entry.url.

I'm just not getting why this error appears, as far as I know, everything should work. I know that there are questions looking similar to mine, but I couldn't figure out the answer. If you want to see the whole source, it's here.

Upvotes: -1

Views: 322

Answers (2)

JJJ
JJJ

Reputation: 33153

The best way to fix the issue is to do it the "jQuery way". Instead of adding a href attribute that executes JavaScript, add a click event:

$('#reactions' + i).click( function() {
    comments( entry.url, i );
});

Similarly don't use document.write() but add elements to the document using jQuery functions.

Upvotes: 0

Dr.Molle
Dr.Molle

Reputation: 117354

Surround entry.url with quotes:

    $('#reactions' + i).attr('href', 'javascript:comments ("'+entry.url+'",'+i+');');

Upvotes: 2

Related Questions