pedram pedram - 4 months ago 8
jQuery Question

jquery not equal in array

I'm trying to find phone number and mobile number from an array with jquery.

jQuery:

var data = $('#PhoneLabel').text();
var array = data.split(', ');
$.grep(array, function (item, index) {
if (item.charAt(0) === '0' && item.charAt(1) === '9') {
var mob = item;
$('#UserMobMenuSMS').attr('href', 'sms:' + mob);
$('#UserMobMenuTel').attr('href', 'tel:' + mob);
alert('its mobile');
} else if (item.charAt(0) !== '0' && item.charAt(1) !== '9') {
$('#UserMobMenuSMS').addClass('disableUserMenuMob');
$('#UserMobMenuSMS').attr('href', '#');
alert('there is no mobile');
} else {
var phone = item;
$('#UserMobMenuTel').attr('href', 'tel:' + phone);
alert('its phone');
}
});


if number start with
09
it is mobile number and else it's phone number. and there is 3 forms in html:

first, only phone number:

Not Working

<span id="PhoneLabel" class="GlobalTelColor">021-88915907 , 021-88915907</span>


second, phone number and mobile number:

Working good

<span id="PhoneLabel" class="GlobalTelColor">09127007008 , 021-55293301-6 , 021-55293003</span>


third, only mobile number:

Working good

<span id="PhoneLabel" class="GlobalTelColor">09195328272</span>


the problem is in first form, if there is no mobile number in array, I want to use phone number in
#UserMobMenuTel
and disable
#UserMobMenuSMS
with
disableUserMenuMob
class. I mean when there is no mobile number
UserMobMenuSMS
should get
disableUserMenuMob
class, but it's not working, i guess i used wrong method in this case, is there any better way and solution?.

please check out the JSFiddle Demo.

Answer

Changed it a bit there. added the disableUserMenuMob classes as intial and then removeClass as you find a mobile. If no mobile found, do nothing.

You had 3 if else switches and I felt 2 is sufficient for what you want

https://jsfiddle.net/m6eza3q1/4/

Comments