admin管理员组

文章数量:1295928

Please before posting or menting ... read and understand that this is inside an application that generates the web page and I cannot create a function I can only edit with will happen inside the onclick

Is it possible to use a "if" function inside an "onclick".

The reason why I have to do this is because this "onclick" is used inside an application that I do not control the code, the only thing I can control is what happens inside the "onclick"

For example:

onclick="document.getElementById('REF_DOC_1_NUMBER').value = '';"

I can write the:

document.getElementById('REF_DOC_1_NUMBER').value = '';

I cannot declare any function in the webpage... cause it's an application that piles the pages... I can only write what is written in the tag onclick...

But not a function. The page is generated by the application itself so I do not control the header.

What I need, a IF that checks an other ID and to change the value ONLY if the value of the other ID (REF_DOC_1_CHOICE) is NA (not applicable)

Any thoughts?

Please before posting or menting ... read and understand that this is inside an application that generates the web page and I cannot create a function I can only edit with will happen inside the onclick

Is it possible to use a "if" function inside an "onclick".

The reason why I have to do this is because this "onclick" is used inside an application that I do not control the code, the only thing I can control is what happens inside the "onclick"

For example:

onclick="document.getElementById('REF_DOC_1_NUMBER').value = '';"

I can write the:

document.getElementById('REF_DOC_1_NUMBER').value = '';

I cannot declare any function in the webpage... cause it's an application that piles the pages... I can only write what is written in the tag onclick...

But not a function. The page is generated by the application itself so I do not control the header.

What I need, a IF that checks an other ID and to change the value ONLY if the value of the other ID (REF_DOC_1_CHOICE) is NA (not applicable)

Any thoughts?

Share Improve this question edited Feb 2, 2014 at 8:02 pSyToR asked Feb 2, 2014 at 7:49 pSyToRpSyToR 9332 gold badges8 silver badges16 bronze badges 2
  • why dont you use function for that – exexzian Commented Feb 2, 2014 at 7:51
  • Learn to read I CAN'T ... – pSyToR Commented Feb 2, 2014 at 7:59
Add a ment  | 

4 Answers 4

Reset to default 3

Yes, you can use any inline statement you want. The onclick event is a function by itself. As you can define one in JS in the header:

document.getElementById('mydiv').onclick = function() {
    var a = 2;
    if (a > 1) {
        // do stuff
    }
};

You can also do so inline:

onclick="var a=2;if(a>1){a=3}else{a=-1}"
onclick="a == 12 && b = true || b = false"

Here's a JSFiddle

though it is not a good practice to use inline javascript but you can use an if in onclick

onclick="document.getElementById('REF_DOC_1_NUMBER').value = '';if(condition){dosomething}else{dosomething else}"

You may use ternary operator. Somehow like this:

onclick="document.getElementById('REF_DOC_1_CHOICE') == 'some_value'? (document.getElementById('REF_DOC_1_NUMBER').value = '') : 0"

Define a function like this :

onclick="function(text){
             // My stuffs
             if(text==='test')
                 document.getElementById('REF_DOC_1_NUMBER').value = '';
        }";

本文标签: javascriptUsing quotifquot inside an quotonclickStack Overflow