I was helping a friend fix some issues with her iPhone connecting to a JavaScript-heavy (and buggy) site. Long story short, her iPhone cannot type brackets ([]), and I need another way of getting the first element in an array. I thought there might be a javascript function like first() that I can access the first element. Nope.

I need something that works like this, without brackets:

myArray[0];

Any thoughts?

Have you tried to copy paste this ( [ ] ) brackets?

I asked her, but I think she's away. Not sure if iPhones can copy and paste. I also can't provide a literal javascript link because the forum we're on blocks javascript links.

Hello,

First, I want to say that you can type [ and ] on the iPhone though I did see that people where having problems typing brackets in some terminal applications.

Second, square brackets are a core component in JavaScript, despite the fact that I was able to create a function for you, you will run into many problems not being able to use them.

Here is the script:

<script type="text/javascript"><!--//

// set your array
var a = new Array("dog","cat","bird")

// prototype function to access the array
Array.prototype.getArrayNum=function(num){var val=this.toString()+",";for(var i=0;i<num;i++){val=val.substring(val.indexOf(",")+1)}return val.substring(0,val.indexOf(","))}


// just type the number of the array element you want 
alert(a.getArrayNum(3))



//--></script>

Enjoy! :)

Thanks for the reply, but that won't work. I thought of converting the array to a string as well, but the array I'm trying to reference is an array of all <form> tags using getElementsByTagName("form") . I can't convert a referenced node to a string.

The only way to access that object without [] would be to give it an id/name or path to it using a node reference such as ...nextSibling from an object you are able to reference.

I would suggest working this problem from another angel and find a way to access the page through another medium. Even if you do solve the immediate, JavaScript relies heavily on arrayed objects and you will likely end up with the same problem all over again.

Best of luck :)

The only way of getting those tags, w/o using any brackets to get its index position/ID is by using this format:

<script type="text/javascript">
<!--

window.onload = function() {
   formByID = (( document.getElementsByTagName ) ? document.getElementsByTagName("form").item("yourFormID") : document.all.tags("form").item( "yourFormID" ));

   formByIndexPos = (( document.getElementsByTagName ) ? document.getElementsByTagName("form").item(0) : document.all.tags("form").item(0));
   alert( formByID.id );
   alert( formByIndexPos.id );

};
 
// -->
</script>

you can use IDS or indexPos to get your elements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.