Welcome to Questionaries, where you can ask questions and receive answers from other members of the community.

Let us know at info@questionaries.org

Javascript Question

8 like 0 dislike
First some background. At present I have both the link and the div that follows it in the HTML my PHP outputs. The link has it's display set to hidden and the div to block. I then use getElementsByClassName to loop through the links and set them to block then loop through the divs and set them to hidden. This is because the links are to toggle the divs display.

Anyway looping through the classnames twice seems excessive to me even when it's narrowed down using the parent node and tag. So I have two options:

1 - Use previousSibling to make the link visible as it will always appear right before the div.
2 - Stop adding the link to the HTML and use insertBefore to create the link just before I hide the div.

The problem with method 2 is I have to assign the div's id to the javascript in the link so it knows which div to hide. I also need to pass an integer that represents how many items are currently in that div. My only solution so far to this has been to put the information in the class attribute such as class="comDiv divID326 divCont4" but, being relatively new to JavaScript, I need to know the best way getting the integers from the classnames. I know I can use element.className to return the contents of class="" but need help on how to parse out the information I need.

or possibly you think leaving the link in the HTML and using previousSibling is the best option.
asked 1 year ago by SeO (39,810 points)

Your answer

Email me at this address if my answer is selected or commented on:
Privacy: Your email address will only be used for sending these notifications.

0 Answers

Related questions

2 like 0 dislike
1 answer
5 like 0 dislike
1 answer
5 like 0 dislike
1 answer
asked 1 year ago by daneim (127,080 points)
4 like 0 dislike
1 answer
4 like 0 dislike
1 answer