ctoz Posted October 13, 2007 Share Posted October 13, 2007 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 More sharing options...
justsomeguy Posted October 15, 2007 Share Posted October 15, 2007 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 More sharing options...
jesh Posted October 15, 2007 Share Posted October 15, 2007 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 More sharing options...
ctoz Posted October 17, 2007 Author Share Posted October 17, 2007 many thanks to both! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.