'how to remove white space in select 2
I had dynamically added the value of the option in select2 as it came from the database but when the field has no value the space can be selected.
How should I remove the spaces? I tried this codes but nothing happens
$imei = $row["IMEI_MX"];
$imeiserial = explode(',', $imei);
<select id="tags"
name="imei"
class="form-control"
onchange="getCount()"
multiple
disabled
>
foreach ($imeiserial as $imeiserial) {
$wew = ltrim($imeiserial, " \t.");
echo '<option value=' . $imei . '>' . $wew . '</option>';
Solution 1:[1]
I'm not entirely sure what your general layout is, but if you're using PHP to generate the HTML select
tag, then you can try something like this:
$imei = $row["IMEI_MX"];
$imeiserial = explode(',', $imei);
echo '<select id="tags" name="imei" disabled class="form-control" onchange="getCount()" multiple>';
foreach($imeiserial as $is){
$wew = trim($is);
if (!empty($wew)) {
echo "<option value='$wew'>$wew</option>";
}
}
echo '</select>';
The PHP empty() function will return true
if the variable is NULL, an empty string, or does not exist. This means that if the value is a space (or empty string once the trim() function has gone through, then it won't output a html <option>
tag.
This is all assuming that you're meant to be creating a separate <option>
for each element in the array after an explode (I'm guessing it's a comma-separated list of IMEIs) but you were outputting the entire string as the value rather than just the element.
e.g.
<option value='867637026628082, 867637026628090'>867637026628082</option>
rather than
<option value='867637026628082'>867637026628082</option>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ScarecrowAU |