TLN_CANADA
asked on
Alter printing ajax variable so it includes if statement
Hi everyone,
Here is an ajax statement that prints the variable reply[0]
$('#myH2').html('Your current value is ' + reply[0]);
I would like to alter what is displayed in the H2 area depending on whether or not the value of the variable is empty.
Here is how I had it working in PHP previously (reply[0] is called $row['question'] here)
Thanks for your help,
D
Here is an ajax statement that prints the variable reply[0]
$('#myH2').html('Your current value is ' + reply[0]);
I would like to alter what is displayed in the H2 area depending on whether or not the value of the variable is empty.
Here is how I had it working in PHP previously (reply[0] is called $row['question'] here)
<h2><?php
if (!empty($row['question']))
{
echo 'Your current value is...' . $row['question'] . ' <img src="change_question_little_button.jpg" alt="Pulpit rock" width="60" height="50">';
}
else
{
echo '<img src="change_question_little_button.jpg" alt="Pulpit rock" width="60" height="50">';
}
?></h2>
Thanks for your help,
D
ASKER
Thanks kozaiwaniec,
I tried this one:
the field in the table is definitely blank at the moment but it is still displaying "Your current value is ".
Thanks for your help,
Derek
I tried this one:
if (reply[0] != "" || reply[0]) {
$('#myH2').html('Your current value is ' + reply[0]);
} else {
$('#myH2').html('something else');
}
the field in the table is definitely blank at the moment but it is still displaying "Your current value is ".
Thanks for your help,
Derek
Hi,
I would try it the other way around:
HTH
Rainer
I would try it the other way around:
if (!reply[0] || reply[0] == null || reply[0] === "") {
$('#myH2').html('something else');
} else {
$('#myH2').html('Your current value is ' + reply[0]);
}
HTH
Rainer
ASKER
Hi Rainer,
It's still coming up with "Your current value is " even though there is no value in the database field.
Cheers,
D
It's still coming up with "Your current value is " even though there is no value in the database field.
Cheers,
D
Could you add a - before and after the output?
I assume that there is a value in there (could be a space)
$('#myH2').html('Your current value is -' + reply[0]+'-');
I assume that there is a value in there (could be a space)
$('#myH2').html('Your current value is -' + reply[0]+'-');
ASKER
If there is a value in the database field, it displays correctly. It's the other part of the statement it doesn't seem response to.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
This works! Thank you.
ASKER
Hi guys,
I have a follow on from this question and if either of you could take a look at it, that would be greatly appreciated.
https://www.experts-exchange.com/questions/28034948/Updating-multiple-values-via-ajax.html
Thanks,
D
I have a follow on from this question and if either of you could take a look at it, that would be greatly appreciated.
https://www.experts-exchange.com/questions/28034948/Updating-multiple-values-via-ajax.html
Thanks,
D
if it's an empty string you can do this:
Open in new window
if it's null or undefined you can do this:
Open in new window
or you can check for both:Open in new window