Skip to main content
April 3, 2015
Answered

How to add an element to an array, then be able to remove it specifically

  • April 3, 2015
  • 1 reply
  • 265 views

So i have a list of numbers. The user is able to then add numbers to that list. I have a button which allows the user to remove any number of their choice by typing that number in a textbox. For some reason the numbers that the user add to the list will not dissapear when chosen whereas the original numbers go away no problem.

The name of the array itself is mesEntiers and the name of the texbox is : IntegersIn_txt

Here is the code :

function supprimer(event:MouseEvent):void

  {

  var indiceChiffre:int;

  indiceChiffre =(mesEntiers.indexOf(IntegersIn_txt.text));

  // La méthode indexOf renvoie l'indice d'un élément ou -1 si non trouvé.

  trace("testBtn")

  if (indiceChiffre != -1)

  {

  for (var i=indiceChiffre; i <mesEntiers.length; i++)

  {

  mesEntiers = mesEntiers[i+1];

  trace("test")

  }

  mesEntiers.pop();

  }

  } // Fin fonction supprimer.

This topic has been closed for replies.
Correct answer Ned Murphy

Two things I can think of (I ran a small test)...

1) Make sure your textfield is a single line, not multiline...  multilines end up with extra stuff in them that you can't see.

2) Convert the text into a number unless the array is holding strings...  indiceChiffre =(mesEntiers.indexOf(Number(IntegersIn_txt.text)));

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
April 3, 2015

Two things I can think of (I ran a small test)...

1) Make sure your textfield is a single line, not multiline...  multilines end up with extra stuff in them that you can't see.

2) Convert the text into a number unless the array is holding strings...  indiceChiffre =(mesEntiers.indexOf(Number(IntegersIn_txt.text)));

April 3, 2015

Awesome! I made the change #2 and converted them into Numbers and it works like a charm now. Thanks a lot!

Ned Murphy
Legend
April 3, 2015

You're welcome