Adding And Removing Array Elements
Jul 18, 2004
Since the Array.splice() method isnt supported by IE5 here's a script with an add function and a remove function.
function remove(nr) {
var nb = parseInt(nr)
for(x=nb;x<myArray.length-1;x++) {
myArray[x] = myArray[x+1]
}
myArray.length += -1
}
function add(nr,value) {
for(x=myArray.length;x>nr;x--) {
myArray[x] = myArray[x-1]
}
myArray[nr] = value
}
You can test it out by including the following html, and an array in the script (here named 'myArray').
<body>
<form>
Nr to add/remove<input type="text" name="nr" /><br />
Value to insert <input type="text" name="val" />
<input type="button" value="remove" onclick="remove(this.form.nr.value)" />
<input type="button" value="add" onclick="add(this.form.nr.value,this.form.val.value)" />
<input type="button" value="View Array" onclick="aA()" />
</form>
</body>
</html>
And this small function
function aA() {
for(x=0;x<myArray.length;x++)
alert(myArray[x])
}
View 11 Replies
ADVERTISEMENT
Jul 4, 2011
I'm trying to make a form that will generate some code for game based on what they fill out. The code will be a modification menu of sorts. They will be able to name the menus and submenus and add or remove them if they want. I will need a way to get the information they filled in for each specific menu form, so I figure giving them each a unique id would do the trick. I'm sure there is a MUCH better way to do this, but here's what I have so far:
[Code]...
View 1 Replies
View Related
Apr 25, 2009
I have a number of DIVs that I want to make 'selectable' on click, which means that I give the DIV you click on a class (just to show it's selected) and save its ID into an array. When I click on that DIV again, the class should be removed and its ID should be removed from the array as well. My code goes like
var selected_items = new Array();
$(".selectableDivs").click(function() {
if($(this).hasClass("selected")) {
[code]....
But the alert at the end of the code always shows my a list of all divs I ever selected, even those that I unselected again. The IDs are just not removed from the array. Is that maybe because items saved in the array are not exactly $(this).attr("id") anymore?
View 2 Replies
View Related
Mar 29, 2009
I have just got myself a copy of SWFUpload to show the progress of file uploads, however, it has a few problems, one of which I am trying to fix with the aid of jQuery. Essentially, if JavaScript doesn't load, then a standard input file element will be shown. But, if JavaScript is enabled, then jQuery removes this, and replaces it with all of the input elements that SWFUpload requires. Is this the best way of doing it, or should I be looking at another option? If so, how would I go about telling jQuery to remove and insert the form field, and each and every attribute the HTML will require?
View 11 Replies
View Related
Sep 7, 2009
I have a page that opens up an iframe for the user to be able to select photos. Each photo has a checkbox, and on select I add a hidden form element to the parent frame form. This all seems to work fine, but im now stuck on how to remove the form element when the checkbox is un-checked.
[Code]...
View 1 Replies
View Related
May 30, 2006
how to do you add elements to an array? am looking at section dealing
with arrays in JS Bible, can't find how you add to an array.. in the
last few years I've been doing much more Java than JS, am used to Java,
where you can't add elements to an array, but use a Vector instead.. and
it's very easy to add to it (use add() method..) how to you add
elements to an array in JavaScript..
I looked up array obj, don't see method to add to array.
View 3 Replies
View Related
Dec 1, 2005
I'm trying to solve a problem with JS textbox array without success.
I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new
textbox named 'dear' is generated. So, if one clicks, say, 3 times the
output is something like:
dear[0]
dear[1]
dear[2]
The length property is accessible; alert(dear.length) gives Ɖ', correctly.
The prolbem is that is not possible to delete a textbox, clicking on MINUS
button. The thing I'd really need to do is to delete from the array (and in
consequence from video) the last textbox created, when one clicks on MINUS
button.
Pop() and Splice() methods do not work, unfortunely...
This is the exact code of my page:
<a href="#plus" onclick="john.innerHTML+='<li><input id=dear type=text
size=50>'">
[PLUS]
</a>
<br>
<a href="#minus" onclick="alert(dear.length);dear.pop()">
[MINUS]
</a>
<ul>
<div id=john>
</div>
</ul>
View 3 Replies
View Related
Mar 23, 2010
Years ago I created HTML that employs checkboxes and textboxes. I am now writing JS with the intention of adding flexibility and limiting redundancy. I am not sure I truly understand how to correctly interact the two though. For example, one of my scripts have arrays that contain the names of the checkboxes and textboxes, with a 'for' loop to document.write() them to references within the HTML code.This does not seem to be working for me though. Here is what I have thus far (in short):
<script language="javascript">
var teamNames = new Array(3);
teamNames[0]="South Africa";
teamNames[1]="Mexico";
teamNames[2]="Uruguay";
[Code]...
I've left out a lot of the code (to include the teamAbbr array, but you get the points. I've tried moving the JS within the HTML body and playing with the reference syntax, but nothing so far.
View 6 Replies
View Related
Feb 5, 2008
I having been trying to make the data from generated (using div tags) pairs of textboxes into two separate arrays, a[] and b[]. I thought that if I label each text box pair a[i] and b[i] that would be enough to include the respective element in each array but that hasn't seemed to work. In fact. the only way I managed to retrieve the values from the text boxes was using "getElementById" and resorted to a for loop in order to make the arrays but that method appears to be able to get elements from only one pair of text boxes. Code:
View 3 Replies
View Related
Jun 25, 2009
I want to be able to add row at the end of the table with a 'remove' link which will remove specific row. The code I've managed to write so far:
$(document).ready(function() {
$("#add_item").click(function () {
var html = "<tr id='item_" + item_id + "' class='item'><td><a
[code]....
The problem is it wont instert the row at the proper place (after the last .item (class) and the remove link wont work for the last row in the table/list.
View 1 Replies
View Related
Apr 10, 2010
I need to remove class to prevent triggering of click function for elements
Have:
<script type="text/javascript">
$(document).ready(function() {
$('.item1').click(function(){
[code]....
second click on element "blabla" triger again click function. why?
View 6 Replies
View Related
Mar 28, 2011
I'm looking to remove an "Href" from a link, but add it back later. So
<
li class="home"><a href="#">Some Click Through</a></li>
"$('.home a').removeAttr('href');"
would remove the href, and the link wouldn't click through, but when I need it to click again what do I do?Do I use attr() some how?
View 2 Replies
View Related
May 18, 2009
I have been doing research on parent object nodes & child nodes. I have a small problem with the code I made. What I need for it to do is depending on which link u click, it adds or deletes the node specified in the function. And the name of the div shouldnt matter as it should be used when clicking on the link <a href="javascript:addevent('divname');">Add Element</a>.
for instance:
"Add element" | "Delete element"
if u click on the add element, then it adds an element, and if u click on the delete element, then it deletes the added element.
Here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
[code]....
View 2 Replies
View Related
Feb 27, 2009
This code was working originally. Then I had made some modifications and things weren't going right so I went back to how it was originally, and now it's not working!What this does is add a file text box when you click a button & then remove it when you click on its link. I get an error though when I click the Remove link saying that its argument is invalid. I don't understand why since its the exact same line of code the Add button has except assigned to a new variable.
This is the line that's coming up with the error: var d = document.getElementById('addToDiv'); in the removeAdditionalFile function. The error message says 'Invalid Argument'.
Here's the javascript:
function addFileTextbox()
{
var a = document.getElementById('addToDiv');
var b = document.getElementById('theValue');
[code]....
View 4 Replies
View Related
Oct 16, 2010
I'm building an application where the user can add Questions. For each Question they can add x answers or remove them (see screenshot). I can add questions and add/remove answers, For each question that is added I update the 'gameQuestions' variable with 1; The same goes for answers that I add: for each added answer I update the 'counter' variable with 1. When I remove an answer, the app. doesn't know how many answers each question has, it simply decreases the "counter" variable. So when I remove the 2nd question, it doesn't know how many answers that 2nd question has! How can I store the number of answers per question and how do I remove them correctly?
Here's my code so far:
$(document).ready(function(){
var gameQuestions = 1;
var counter = 1;
// Add a New Question...
$(".addQuestion").click(function() {
var question = "<table id='questionSet"+gameQuestions+"' class='form-table' style='background-color:#cccccc; width=100%;'>"; .....
View 1 Replies
View Related
May 19, 2009
I have a list of text-snippets which should be inserted into a textarea, separated by a comma. When I click the same text-snipped
again, it should be removed from the textarea. I managed to to implement the "inserting" with the following code,
$("a.tags2textarea").click(function()
{
$("textarea.txt_tags").append($(this).html()+", ");
[code]....
View 1 Replies
View Related
May 5, 2011
This is essentially the effect I'm going for:
function quickadd() {
$('.menu').addClass('red').delay(1000).removeClass('red');
}
So, from what I've gathered from the docs, this doesn't work because the addClass and removeClass functions don't observe the queue. How could I get around this and setup a function that will add a class, wait a second, then remove that class? Would it require me to go outside of jQuery a bit and use some standard JS?
View 1 Replies
View Related
Aug 30, 2011
I would like to have two columns, the left column showing all available items with an add button. The right column showing all of the added items, and then adding these together to provide an order total, but with the ability for users to add to the quantity or remove from their order.I have the add item to order bit working on, and the sub-total is working.I started to try to add on add/remove buttons to the right #your-order column but am getting stuck.Every time a user adds another item to their order, more buttons are added, and I can't get the "remove item" button working.The page is located here:URL...Should I paste the jQuery code I am using here?
View 3 Replies
View Related
Aug 11, 2011
I have a lot of information on my site that is in multiple categories. I have a list of categories, each with its own checkbox. By default, all the information is displayed (therefore all the checkbox's are checked by default).When someone unchecks the box I want to hide the divs with that class, and when someone rechecks the box, they should reappear. Here is my code so far. It is hiding the div's fine, but they are not reappearing when user tics the checkbox again.
<script type="text/javascript">
function toggledisplay(category){
if(document.getElementById(category).checked){
[code]....
View 2 Replies
View Related
Sep 15, 2011
I've been learning a lot about jscript and PHP while I've been building my site...now I'm curious about using a little ajax. I have an internal email setup on my site where users can contact one-another...when you click on Inbox, Sent, Drafts, ect it calls a php script and reloads the page. I'm considering for my next release, doing this with jscript but my question is this...
When I print info to the page that I'm on, how hard is it to get rid of the old data that was there? This might be the easiest thing in the world but i don't even know what to call this on google. Also, if that's the norm, how hard is it to add data to a page with out getting rid of whats already there? I've been reading about ajax and I've been using jscripts to check my forms and do simple things but I'm really interested in learning what I really have the power to do here...
View 5 Replies
View Related
Feb 17, 2006
The problem is that I'm creating child elements based on an xml document served from a php script. However everytime it is served I'm just appending all the elements to the end of the child list creating copies. So I decided to just remove all the children each time.. But this doesn't seem to be working.
function remove_shouts(){
var shoutbox_div = document.getElementById('shouts');
var shouts = shoutbox_div.childNodes;
for(var i = 0; i < shouts.length; i++){
shoutbox_div.removeChild(shouts.item(i));
}
}
View 4 Replies
View Related
Dec 9, 2011
I put this at the end of my code thinking that after the div displays (id = 'link_container') I can eliminate (id = 'link_container') from memory when the script ends so there's a place for another div (id = 'link_container') when/if there's onkeyup event. Wouldn't this little script remove any element with an id = 'link_container' ?
<script type="text/javascript">
var deleteDiv = document.getElementById('link_container');
document.body.removeChild(deleteDiv);
</script>
View 14 Replies
View Related
Nov 25, 2011
how I can use Jquery to append and remove data into a table by checking and unchecking html checkboxes?(Or any plugin for this?) I would like to generate an online order invoice from the following checkboxes:
<
fieldset
>
<
[Code].....
I find these two example on the net but (The first example looks too similar to what I would like to do but with a table) but Icouldn'tfigure out how to modify them? [URL]
View 11 Replies
View Related
Mar 8, 2011
I'm trying to get my script below working. I've managed to get it to add fields dynamically but the remove function isn't right... and I can't think of a way round it. how can i incorporate a way for a limit on the number of fields that can be added?
<script type="text/javascript">
function addInput()
{
var x = document.getElementById("inputs");
x.innerHTML += "<input type="text" />";
}
function removeInput()
{
[Code].....
ah, i've just looked into the operators in more details and -= is just for numbers... so this won't work. i presume this is going to be the wrong method then.
View 3 Replies
View Related
Feb 15, 2010
I have a block of HTML that I am cloning, as I want to manipulate it to remove an element, so that I can then append that new HTML somewhere else on the page.
[Code]....
Then in the jQuery, I have tried many different ways of removing stuff from that clone, but with no luck. E.g.
var cont = $('#container').clone();
$(cont).remove("copy_btn").appendTo("body");
There are no errors showing up in Firebug, but the html added to the bodu still contains the link with the id "copy_btn". Is this even possible? The api says that remove() will remove matched selectors from the DOM, so as this is just a set of elements is that the reason? If so, is there a way to achieve this, or would I have to append it somewhere on my page and then perform the manipulation?
View 4 Replies
View Related
Mar 24, 2011
I am creating textboxes dynamically using the following code:
[Code]...
I want to have a function that will remove textboxes and the newline that is created. I have the following function to remove the text boxes but I cant remove the new lines that were created so an empty space is left on the page.function RemoveElement() {
var d = document.getElementById('spanManualInput');
var oldbox = document.getElementById('Value'+i);
d.removeChild(oldbox);
d.removeChild('br');
}
View 7 Replies
View Related