user1394925
user1394925

Reputation: 752

$("<div/>").text(value).html is not a function error

I am getting an $("<div/>").text(value).html is not a function error in the code below:

function htmlEncode(value) { 
    return $('<div/>').text(value).html(); 
}

function startImageUpload(imageuploadform, imagefilename){

    $('.imagef1_cancel').eq(window.lastUploadImageIndex).html(
        '<div>' + 
        htmlEncode(imagefilename) + 
        '<button type="button" class="imageCancel" cancel_image_file_name="' + 
        imagefilename + 
        '">CANCEL</button></div>'
    );

    return true;
}

How can this error be fixed?

Upvotes: 0

Views: 3667

Answers (3)

coolguy
coolguy

Reputation: 7954

IM not sure wat you want but definitely the error is in this line

 return $('<div/>').text(value).html(); 

If you want to wrap that imagevalue in a div, simply use|

function htmlEncode(value) { 
    return '<div><img src="'+value+'"/></div>'; //wrapping  it as an image most probably you want this
}

Upvotes: 0

Guffa
Guffa

Reputation: 700472

The code works fine by itself:

http://jsfiddle.net/Guffa/aae24/

You should check for anything strange in the string that you send into the function, that might mess up the code that is created. I have tried some different values though, and you can easily break the resulting markup (as you only html encode the value in one place, not the other), but the htmlEncode function still seems to run without an error message whatever you throw at it.

You can also check that the $.html property actually still is a function, so that you haven't accidentally changed it somewhere. The string value returns the function code:

alert($().html);

Upvotes: 2

Qpirate
Qpirate

Reputation: 2078

the output of

$('<div/>').text(value)

is a String and not a Jquery Object.

have you tried this

$($('<div/>').text(value)).html();

Upvotes: 4

Related Questions