Forum Moderators: open

Message Too Old, No Replies

Replace Form w/ alt content

         

nibra

8:55 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



I have a form, with a class (not an ID) with the name submit-answers. I'm attempting to replace the HTML with a flash plug-in. How best do I do this? I found several tutorials online that show various getElementByID methods, but since this particular form doesn't have an ID, but rather a class, I'm at a complete loss. (Also, it doesn't help that I'm a complete javascript n00b)

<form class="submit-answers>
<div>
...
</div>
</form>

Demaestro

8:59 pm on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there a reason you can't assign an id to it?

Seems like that would solve the problem.

Adding an id will not mess up the class unless there is a defined css id/class of the same name... just make sure it is a unique name and it should be fine.

<form class="submit-answers" id="submit_answers_form">
<div>
...
</div>
</form>

Just make sure the ID doesn't have any '-' in them as that can get confused as an operator by code.

[edited by: Demaestro at 9:01 pm (utc) on Dec. 18, 2007]

nibra

9:29 pm on Dec 18, 2007 (gmt 0)

10+ Year Member



An ID cannot be added. This is supposed to be a drop-in and play plugin for an application. Asking users to modify the source, so that this replacement will work, isn't going to work.

This is the only instance where this class can be found, luckily enough.

Demaestro

9:53 pm on Dec 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try this

/*
Written by Jonathan Snook, [snook.ca...]
Add-ons by Robert Nyman, [robertnyman.com...]
*/

function getElementsByClassName(oElm, strTagName, strClassName){
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-");
var oRegExp = new RegExp("(^¦\\s)" + strClassName + "(\\s¦$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}

Some ways to call it.....

To get all a elements in the document with a “info-links” class.
getElementsByClassName(document, "a", "info-links");

To get all div elements within the element named “container”, with a “col” class.
getElementsByClassName(document.getElementById("container"), "div", "col");

To get all elements within in the document with a “click-me” class.
getElementsByClassName(document, "*", "click-me");