admin管理员组

文章数量:1026989

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?

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?

Share Improve this question edited May 15, 2012 at 12:31 user1394925 asked May 15, 2012 at 10:28 user1394925user1394925 7529 gold badges29 silver badges51 bronze badges 7
  • what is '<div/>' in $('<div/>').text(value).html(); – Adam Commented May 15, 2012 at 10:32
  • what exactly you are trying to return here $('<div/>').text(value).html(); – Siva Charan Commented May 15, 2012 at 10:32
  • It is just a div tag it is returning so that the value is displayed in its own div tag – user1394925 Commented May 15, 2012 at 10:33
  • I actually didn't get any error when running your code, but why not just concatenate the filename to the div like that: '<div>' + imageFileName + '</div>', why do you need special method for that? – Naama Katiee Commented May 15, 2012 at 10:40
  • @NaamaKatiee Because I need to get the name of the file and return it and it seems to do it with the html.encode. If you look at the second code I added then it works with a delete button, it just doesn't work with the cancel button – user1394925 Commented May 15, 2012 at 10:48
 |  Show 2 more ments

3 Answers 3

Reset to default 4

the output of

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

is a String and not a Jquery Object.

have you tried this

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

The code works fine by itself:

http://jsfiddle/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);

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
}

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?

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?

Share Improve this question edited May 15, 2012 at 12:31 user1394925 asked May 15, 2012 at 10:28 user1394925user1394925 7529 gold badges29 silver badges51 bronze badges 7
  • what is '<div/>' in $('<div/>').text(value).html(); – Adam Commented May 15, 2012 at 10:32
  • what exactly you are trying to return here $('<div/>').text(value).html(); – Siva Charan Commented May 15, 2012 at 10:32
  • It is just a div tag it is returning so that the value is displayed in its own div tag – user1394925 Commented May 15, 2012 at 10:33
  • I actually didn't get any error when running your code, but why not just concatenate the filename to the div like that: '<div>' + imageFileName + '</div>', why do you need special method for that? – Naama Katiee Commented May 15, 2012 at 10:40
  • @NaamaKatiee Because I need to get the name of the file and return it and it seems to do it with the html.encode. If you look at the second code I added then it works with a delete button, it just doesn't work with the cancel button – user1394925 Commented May 15, 2012 at 10:48
 |  Show 2 more ments

3 Answers 3

Reset to default 4

the output of

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

is a String and not a Jquery Object.

have you tried this

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

The code works fine by itself:

http://jsfiddle/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);

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
}

本文标签: javascript(quotltdivgtquot)text(value)html is not a function errorStack Overflow