Jump to content

alternative to charAt ?


ctoz
 Share

Recommended Posts

This function (built here with help)

function movD() {for (i=0;i<document.getElementsByTagName("span").length; i++) {if (document.getElementsByTagName("span").item(i).id.charAt(0) == "m"){document.getElementsByTagName("span").item(i).style.display = "inline"  }}}

grabs elements with "m" as the first character in their id.If I need to grab elements by the first and second characters in the id, say "m1", is it

if (document.getElementsByTagName("span").item(i).id.charAt(0) == "m" &&if (document.getElementsByTagName("span").item(i).id.charAt(1) == "1)) { etc

or is there a way of converting the id to a string and then choosing the relevant bit? The id would have five characters.Cheers. Brain's gone to jelly.

Link to comment
Share on other sites

The ID already is a string, you can use either substr or substring to get part of the string. What you have would work but you don't put one if statement as a condition inside another if, you just list all of the conditions separated by either an && or || operator.

Link to comment
Share on other sites

You could also use indexOf rather than charAt

function movD(){	var spans = document.getElementsByTagName("span");	for (i=0; i<spans.length; i++)	{		if(spans[i].id.indexOf("m1") == 0)		{			spans[i].style.display = "inline";		}	}}

Check out the Javascript String reference: http://www.w3schools.com/jsref/jsref_obj_string.asp

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...