Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7191315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:42:10+00:00 2026-05-28T19:42:10+00:00

I am working on a Phonegap app, in which I access device’s contacts. I

  • 0

I am working on a Phonegap app, in which I access device’s contacts. I then store upto 10 contacts in window.localStorage. To do so, when the user select a button, I create a div which has three elements.

  1. An image (contact icon that represents male/female contact)
  2. The name of the contact
  3. Another image (represents ‘add’ sign to add it to window.localStorage)

I then associate an event handler, which will first check if the contact already exists in the localStorage and then proceed to add the contact. Here is the code

function checkDuplicate(somevalue)
{
    for(var i=0;i<10;i++) {
        if(window.localStorage.getItem(i)!=null) {
            if(window.localStorage.getItem(i)==somevalue) {
                navigator.notification.alert('Entry exists at Button:'+i);
                return false;
            }
        }
    }
    //chosenButton is a global variable
    window.localStorage.setItem(chosenButton,somevalue);
    document.getElementById('contactNumberField').textContent=somevalue;
}

//Problem is with the event listener attached to span2. Please read below

function addContact(item)
{
    var parentDiv = document.getElementById('thelist');
    var childDiv = document.createElement('li');
    var span1 = document.createElement('span');
    span1.style.float='left';
    span1.innerHTML = "<img src='keypad-contact.png'/>";
    var span2 = document.createElement('span');
    span2.style.float='right';
    span2.innerHTML="<img src='keypad-addcontact.png'/>";
    span2.addEventListener('click',function({checkDuplicate(item.number);},false);
    childDiv.textContent=item.name;
    childDiv.style.color='white';
    childDiv.appendChild(span1);
    childDiv.appendChild(span2);
    parentDiv.appendChild(childDiv);    
}

function onSuccess(contacts)
{
    var objArray = new Array();
    for(var i=0; i<contacts.length;i++) {
        var tempObj = new Object();
        tempObj['name']=contacts[i].displayName;
        tempObj['number']=contacts[i].phoneNumbers[0].value;
        objArray.push(tempObj);                 
    }
    objArray.sort(
    function(a,b){
        var nameA = a.name.toLowerCase(),nameB=b.name.toLowerCase();
        if(nameA < nameB) return -1;
        else if(nameA > nameB) return 1;
        return 0;
    }); 
    for(var i=0; i<objArray.length;i++) addContact(objArray[i]);
}

function onDeviceReady()
{
    var options = new ContactFindOptions();
    options.multiple=true;
    var field = ["displayName","phoneNumbers"];
    navigator.contacts.find(field, onSuccess, function(){alert('NA');}, options);
}

Problem

When I try to add a contact which is already present in window.localStorage, for my first touch on span2, I get one alert. If I try to add again by touching it for the second time, I get two alerts.. and this goes on. The trouble is with the event handler associated with span2. However, I don’t know how to over come this situation. How can I ensure that irrespective of how many times I press span2, I get alert only once. How to remove the event handler as soon as it is fired?

Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:42:11+00:00Added an answer on May 28, 2026 at 7:42 pm

    Fixed:
    The issue was not with adding event handlers dynamically. I happen to use iScroll (cubiq) for my project and that caused the trouble. Now I am not receiving multiple alerts. The trick is to declare the globlal scroller variable only once.

    var scroller = null;
    

    and then in the function

    if(!scroller) scroller = new iScroll('scrollableDiv');
    

    I hope this could be a useful tip for people who are using iScroll and struggling with multiple alerts like me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a phonegap iPhone app which downloads content from the network in
I'm using ShareKit to post to Facebook from a PhoneGap app which I'm working
Am making a basic phonegap based android app. All working perfectly and using localstorage
I'm working on a mobile app with Phonegap which will use Geolocation. I used
I am working on phonegap app. I need to validate user through .net webservice.
I am working on a HTML5 app which uses PhoneGap and Backbone JS. Problem
We've put together an app that is mostly working in phonegap. We have an
I have been working on an Android app using Phonegap and now would like
I am working on an app for android using phonegap and unity. I wrote
I'm developing an iOS app. Its working properly on simulator. I'm using phonegap and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.