0

I can't work out this Javascript for the life of me. I need help with the following code, here is the snippet:

<div class="container">
  <input type="submit" class="clicktoshow" />

  <form class="hidden">
  </form>
</div>

I am going to be using an array in PHP to display multiple containers using the same code so I want to avoid id's.

Basically the form is hidden and I want it to show when submit is clicked, there will be alot of container divs, so I don't want the click to show ALL the forms, just the one aligned with the input / in its parent element.

Is this possible?

Any help please :|?

1

3 Answers 3

1

Try this code

document.getElementsByClassName('clicktoshow')[0].onclick = function(Event){
     var form = Event.target.parentNode.children[1]; //form object 
     form.style.display = "block"; // form node action
};

If the class repeated in code then you will need to iterate event code Like

var cls = document.getElementsByClassName('clicktoshow');
for(var i in cls){
  cls[i].onclick = function(Event){ 
   var form = Event.target.parentNode.children[1]; //form object 
    form.style.display = "block"; // form node action
 };

}

DEMO

NOTE : The code will work in modern browsers.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, could you please emphasis on //your code here? What do I do to get the form to work in the script? Thanks!
@user3405517 check updated code as well as demo link, hope the code will be helpful,
It worked! Thanks a bunch! If i could please bother you for one more question, is there a way I can make it disappear :|?
0

Yes you can do this....

var element = document.getElementsByClassName('clicktoshow');

element.onclick = function(){
  var parent = element.parentNode;
  var form = parent.getElementByClassName('hidden');
}

Comments

0

*Yes try this*

var clickClass= document.getElementsByClassName('clicktoshow');

clickClass.onclick = function(){
  // your code here
}

more info

more info link 2

4 Comments

When I put my code inside the function do I need to add anything? I'm sorry if i sounds blatantly noob, I am learning Javascript!
Yes more about info than go here developer.mozilla.org/en-US/docs/Web/API/…
var clickClass= Document.getElementsByClassName('clicktoshow'); clickClass.onclick = function(){ <form> Show Content </form> } It doesn't seem to run am i missing something :\
now go this link and read carefully all topic about javascript developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.