Henry
Henry

Reputation: 883

JScript error: Cannot assign to a function result

In my application I am trying to hide a table based on a condition. I am using simple javascript function to hide the table.

the function is failing on the following line

if ((image1.trim() = '') && (image2 != '1')) {

giving error

'Microsoft JScript runtime error: Cannot assign to a function result'.

Here is the code.

Html code:

 <table id="tblImage" cellpadding="0" cellspacing="0" style="padding:2px 0px">
            <tr>                
                <td>
                    <div id="otherImages"></div>
                </td>
            </tr>
     </table>   

Javascript function:

function DisplayTable() {
    var image1 = document.getElementById('ctl00_ContentPlaceHolder1_image1').value;
    var image2 = document.getElementById('ctl00_ContentPlaceHolder1_image2').value;
    if ((image1.trim() = '') && (image2 != '')) {
         jQuery('#tblImage').hide();
    }
}

Upvotes: 2

Views: 15485

Answers (3)

Shmiddty
Shmiddty

Reputation: 13967

if ((image1.trim() = '') && (image2 != '')) {

should be

if ((image1.trim() == '') && (image2 != '')) {
              this__^

Upvotes: 2

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91299

You are using = (assignment) instead of == (equals) in the folowing if statement, resulting in the assignment to a function error. Use the following instead:

if ((image1.trim() == '') && (image2 != '')) {
     jQuery('#tblImage').hide();
}

Upvotes: 2

Zbigniew
Zbigniew

Reputation: 27584

You are using =, but you should use ==:

if ((image1.trim() == '') && (image2 != '1')) {

Basicaly = means assign value and == means is equal to. This generates error because it is not possible to assign value to function (which happens where, you're trying to assign value to trim()).

Upvotes: 4

Related Questions