0

First of all I get all the values from a php array:

<?php
  $user_id = get_current_user_id();
  $userPostsInternal = get_user_meta( $user_id, 'save_post_internal', TRUE );
  $userPostsExternal = get_user_meta( $user_id, 'save_post_external', TRUE )
?>

I then get those arrays and convert them in a JS array

var savedInternal = "<?php echo $userPostsInternal; ?>";
var savedExternal = "<?php echo $userPostsExternal; ?>";

savedInternal = savedInternal.split(',');
savedExternal = savedExternal.split(',');

I then need to check if current id value is in the js array and proceed accordingly:

if($.inArray(this.id, savedInternal) !== -1) {
    console.log("yes");               
} else {
    console.log("no");  
}

This is happening on a mouse over an element, if I place the following the id is correct, so it isn't about this.id

console.log(this.id);

I get 128545 and it is correct.

Full code:

google.maps.event.addListener(circle, 'mouseover', function(e) {

  <?php
    $user_id = get_current_user_id();
    $userPostsInternal = get_user_meta( $user_id, 'save_post_internal', TRUE );
    $userPostsExternal = get_user_meta( $user_id, 'save_post_external', TRUE )
  ?>

  var savedInternal = "<?php echo $userPostsInternal; ?>";
  var savedExternal = "<?php echo $userPostsExternal; ?>";

  savedInternal = savedInternal.split(',');
  savedExternal = savedExternal.split(',');

  $("#timeSearch").removeClass("fadeIn").addClass("fadeOut");
  $(".infoBox").removeClass("fadeOut").addClass("fadeIn");
  if(this.currSite == "curr" ) {
    var linkGo = this.linkToPost;
    var whatSite = this.currSite;
    if($.inArray(this.id, savedInternal) !== -1) {
      var contentString = '<div class="row infoBox"><div class="col"><p>' + this.site + '</p><hr><h5>'+this.title+'</h5><hr><p><button data-whatSite="'+whatSite+'" data-id="'+this.id+'" type="button" class="btn-site btn btn btn-outline-dark btn-block">Già nella box</button></p><hr><a class="d-block margin-top-20 btn btn-outline-dark btn-block" href="'+linkGo+'">Vedi contenuto</a></div></div>';                  
    } else {
      var contentString = '<div class="row infoBox"><div class="col"><p>' + this.site + '</p><hr><h5>'+this.title+'</h5><hr><p><button data-whatSite="'+whatSite+'" data-id="'+this.id+'" type="button" class="btn-site btn btn btn-dark btn-block">Salva nella box</button></p><hr><a class="d-block margin-top-20 btn btn-outline-dark btn-block" href="'+linkGo+'">Vedi contenuto</a></div></div>';                  
    }
  } else { 
    var linkGo = linkExternal+this.linkToPost;
    var whatSite = this.currSite;
    if($.inArray(this.id, savedExternal) !== -1) {
      var contentString = '<div class="row infoBox"><div class="col"><p>' + this.site + '</p><hr><h5>'+this.title+'</h5><hr><p><button data-whatSite="'+whatSite+'" data-id="'+this.id+'" type="button" class="btn-site btn btn btn-outline-dark btn-block">Già nella box</button></p><hr><a class="d-block margin-top-20 btn btn-outline-dark btn-block" href="'+linkGo+'">Vedi contenuto</a></div></div>';                  
    } else {
      var contentString = '<div class="row infoBox"><div class="col"><p>' + this.site + '</p><hr><h5>'+this.title+'</h5><hr><p><button data-whatSite="'+whatSite+'" data-id="'+this.id+'" type="button" class="btn-site btn btn btn-dark btn-block">Salva nella box</button></p><hr><a class="d-block margin-top-20 btn btn-outline-dark btn-block" href="'+linkGo+'">Vedi contenuto</a></div></div>';                  
    }
  }
  infoWindow = new google.maps.InfoWindow({content: contentString});
  infoWindow.setPosition(this.getCenter());
  infoWindow.open(map);
  btnBoxSave(infoWindow, whatSite);
});
9
  • $.inArray uses strict comparison, make sure this.id is the same type as the values in the arrays. Commented Aug 4, 2019 at 14:44
  • @Titus looks like I cannot get access to the js array on mouse hover in gmap as if I do console.log(savedInternal); I get nothing yet if I place the whole array bit before the hover states in gmaps, I get the correct values Commented Aug 4, 2019 at 14:45
  • $userPostsInternal and $userPostsExternal are php arrays ? Commented Aug 4, 2019 at 14:47
  • Try: $.inArray(this.id.toString(), savedInternal), do the same for the other check. Commented Aug 4, 2019 at 14:48
  • @FrançoisHuppé yes indeed Commented Aug 4, 2019 at 14:48

1 Answer 1

1

$.inArray(...) uses strict comparison. From the code that you've posted it seems that the arrays contain strings (they were created using .split(...) which returns an array of strings) and the value you're checking to see if it is in the arrays (this.id) is a number.

To fix that, use:

$.inArray(this.id.toString(), savedInternal) and $.inArray(this.id.toString(), savedExternal)

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant hanks a lot, makes total sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.