This is a migrated thread and some comments may be shown as answers.

What is the problem in this code block?

6 Answers 75 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Hatice
Top achievements
Rank 1
Hatice asked on 19 Jun 2013, 12:12 PM
var kon = -1;
var a = 'Tam Sitogenetik Yanıt';
var b = 'Kısmi Sitogenetik Yanıt';
var c = 'Minör Sitogenetik Yanıt';
var d = 'Minimal Sitogenetik Yanıt';
var e = 'Yanıt Yok';

kon = $find('cid46889').get_value();

if ($find('cid46889').get_value() != '')
{
if (kon ==  0)  $find('cid47267').set_value(a); 
if (kon >= 1 && kon <= 35) $find('cid47267').set_value(b); 
if (kon >= 36 && kon <= 65) $find('cid47267').set_value(c); 
if (kon >= 66 && kon <= 95) $find('cid47267').set_value(d); 
if (kon > 95) $find('cid47267').set_value(e); 
}
else $find('cid47267').set_value(''); 
----------------------------------------------------------------------
when i enter 0 in textbox, it is return empty. but it must show 'Tam Sitogenetik Yanıt'
it is not giving error but right text is not returning back.

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 19 Jun 2013, 12:53 PM
Hi,

I tried the same code and it worked as expected at my end. Are you using RadTextBox? Here I am giving the code that use RadTextBox and asp TextBox.
JS:
function Click()
 {
        var kon = -1;
        var a = 'Tam Sitogenetik Yanıt';
        var b = 'Kısmi Sitogenetik Yanıt';
        var c = 'Minör Sitogenetik Yanıt';
        var d = 'Minimal Sitogenetik Yanıt';
        var e = 'Yanıt Yok';
 
       kon = $find('cid46889').get_value(); //if RadTextBox
       // kon = document.getElementById("cid46889").value; // asp TextBox
      var cid47267 = $find('cid47267'); //if RadTextBox
       //  var cid47267 = document.getElementById("cid47267"); // asp TextBox
      if (kon != '')
       {
        if (kon == 0) cid47267.set_value(a);
        if (kon >= 1 && kon <= 35) cid47267.set_value(b);
        if (kon >= 36 && kon <= 65) cid47267.set_value(c);
        if (kon >= 66 && kon <= 95) cid47267.set_value(d);
        if (kon > 95) cid47267.set_value(e);
     }
    else
     {
        cid47267.set_value('');
      }
}

Thanks,
Princy.
0
Hatice
Top achievements
Rank 1
answered on 19 Jun 2013, 01:08 PM
Hi Princy,

Thank you so much for the answer.

But codes are working, for 1 between 95. only 0 is not working.
when i enter 0, it is returning empty. maybe it is about string or integer expression, i dont know.
0
Princy
Top achievements
Rank 1
answered on 24 Jun 2013, 05:38 AM
Hi,

I'm sorry i tried to replicate the issue but no avail.Here is my full code.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" >
  . . . . . . . . . . . . . . . .  
</telerik:RadGrid>
  <telerik:RadTextBox ID="cid46889" runat="server">
  </telerik:RadTextBox>
  <telerik:RadTextBox ID="cid47267" runat="server">
  </telerik:RadTextBox>   
<asp:Button ID="Button1" runat="server" Text="Ok" OnClientClick="return Click();" />

JS:
<script type="text/javascript">
    function Click() {
        var kon = -1;
        var a = 'Tam Sitogenetik Yanıt';
        var b = 'Kısmi Sitogenetik Yanıt';
        var c = 'Minör Sitogenetik Yanıt';
        var d = 'Minimal Sitogenetik Yanıt';
        var e = 'Yanıt Yok';
 
        kon = $find('cid46889').get_value();
        var cid47267 = $find('cid47267');
        if (kon != '')
        {
            if (kon == 0) cid47267.set_value(a);
            if (kon >= 1 && kon <= 35) cid47267.set_value(b);
            if (kon >= 36 && kon <= 65) cid47267.set_value(c);
            if (kon >= 66 && kon <= 95) cid47267.set_value(d);
            if (kon > 95) cid47267.set_value(e);
        }
        else
        {
            cid47267.set_value('');           
        }  
    }
</script>

Thanks,
Princy
0
Hatice
Top achievements
Rank 1
answered on 26 Jun 2013, 10:12 AM
Hi,

Thanks a lot Princy,

i chanced codes with switch-case, like this;

var fish;
var a, b, c, d, e;
a = 'Tam Sitogenetik Yanıt';
b = 'Kısmi Sitogenetik Yanıt';
c = 'Minör Sitogenetik Yanıt';
d = 'Minimal Sitogenetik Yanıt';
e = 'Yanıt Yok';

fish = $find('cid46890').get_value();

switch (true)
{
case (fish == 0):
$find('cid47268').set_value(a);

break;

case (fish  >= 1 && fish  <= 35):
$find('cid47268').set_value(b);
break;

case (fish  >= 36 && fish  <= 65):
$find('cid47268').set_value(c);
break;

case  (fish  >= 66 && fish  <= 95):
$find('cid47268').set_value(d);
break;

case (fish  > 95):
$find('cid47268').set_value(e);
break;

case (fish == ''):
$find('cid47268').set_value(''); 
break;

}

now 0 is working, when i enter 0 it is showing 'Tam sitogenetik yanıt'. but now my problem is when textbox is null, it is showing 'Tam sitogenetik yanıt' . it must show empty when textbox is null.
how can i change texts when enter 0 or null?

Thanks,
0
Accepted
Shinu
Top achievements
Rank 1
answered on 27 Jun 2013, 05:36 AM
Hi Hatice,

According to the Mozilla documentation on JavaScript Comparison Operators, if the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers; if either operand is a string, the other one is converted to a string.

What's actually happening is that the strings are being converted to numbers. For example:

1 == '1' becomes 1 == Number('1') becomes 1 == 1: true

Then try this one: 1 == '1.' becomes 1 == Number('1.') becomes 1 == 1: true If they were becoming strings, then you'd get '1' == '1.', which would be false.

It just so happens that Number('') == 0, therefore 0 == '' is true and this happens in your case. So when you explicitly parse as parseInt($find('cid46890').get_value()) as given in my code, the variable fish has value NaN (Not a Number) if the textbox is empty and case(fish==0) wont get executed since NaN not equals 0 and the code works properly as expected.


Please try the following JavaScript code which works fine at my end.

JavaScript:
<script type="text/javascript">
    function OnClientClicked() {
        var fish;
        var a, b, c, d, e;
        a = 'Tam Sitogenetik Yanıt';
        b = 'Kısmi Sitogenetik Yanıt';
        c = 'Minör Sitogenetik Yanıt';
        d = 'Minimal Sitogenetik Yanıt';
        e = 'Yanıt Yok';
 
        fish = parseInt($find('cid46890').get_value());
        switch (true) {
            case (fish == 0):
                $find('cid47268').set_value(a);
                break;
 
            case (fish >= 1 && fish <= 35):
                $find('cid47268').set_value(b);
                break;
 
            case (fish >= 36 && fish <= 65):
                $find('cid47268').set_value(c);
                break;
 
            case (fish >= 66 && fish <= 95):
                $find('cid47268').set_value(d);
                break;
 
            case (fish > 95):
                $find('cid47268').set_value(e);
                break;
 
            default:
                $find('cid47268').set_value("");
        }
    }
</script>

Thanks,
Shinu.
0
Hatice
Top achievements
Rank 1
answered on 02 Jul 2013, 08:18 AM
Hi Shinu and Princy,

Thank you so much for your help. it is already completed because of you. a lots of thanks for your useful informations.

Thanks,
Tags
General Discussions
Asked by
Hatice
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Hatice
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Share this question
or