0

Should be simple and clear, but I dont get what is wrong here. Simple javascript show/hide content. Tryed both display - block/none and visibility - show/hidden. Not working.. It works if all divs visibility named npctext_X are set to visible. but i need them to be hidden, becouse this will be used in as dialogues...

<script type="text/javascript">

function npcfunkc(karodyt,senas){

var showit = 'npctext_'+karodyt;
var hideit = 'npctext_'+senas

//document.getElementById(showit).style.display='block';
//document.getElementById(hideit).style.display='none';

document.getElementById(showit).style.visibility = 'show';
document.getElementById(hideit).style.visibility = 'hidden';
}
</script>


<div class='npctalk'>

<div id='npctext_1' style='visibility:visible;'>text 1 text 1 text 1 <br /><br />
    <button onclick='npcfunkc(2,1)'>show 2 [hide 1]
    </button><br />
    <button onclick='npcfunkc(3,1)'>show 3 [hide 1]
    </button><br />
</div>

<div id='npctext_2' style='visibility:hidden;'>text 2 text 2 text 2 <br /><br />
    <button onclick='npcfunkc(1,2)'>show 1, [hide 2]
    </button><br />
    <button onclick='npcfunkc(3,2)'>show 3, [hide 2]
    </button><br />
</div>

<div id='npctext_3' style='visibility:hidden;'>text 3 text 3 text 3 <br /><br />
    <button onclick='npcfunkc(2,3)'>show 2, [hide 3]
    </button><br />
    <button onclick='npcfunkc(1,3)'>show 1, [hide 3]
    </button><br />
</div>

</div>
3
  • Welcome to stackoverflow! Please refer the faq section for any help regarding markup. I edited you code indention for markup. -- stackoverflow.com/editing-help Commented Nov 16, 2011 at 22:26
  • Is there a particular reason why your not using a JS library like YUI or jQuery? Commented Nov 16, 2011 at 22:45
  • the script works for me as i have tried using display:block/none Commented Nov 17, 2011 at 0:23

2 Answers 2

1

As a best-practice and potential solution to your problem, you should use CSS classes and add or remove those classes from your objects.

This Tutorial may help you.

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

3 Comments

How is it a 'best practice' to add or remove classes from objects to hide or show them?
@blankabout it's better to use classes than directly setting style properties of the object. How to implement the actual hiding or showing of elements is left to the developer to decide.
Whilst using CSS rather than inline styles is good practice, you should make it clear in your reply that adding or removing classes from an object is certainly not a good way of hiding or showing it which is what the question was about.
0

mYou are sending integers to your function. Try wrapping them with quotes like this to convert them to string....

npcfunkc("2", "1")

Comments

Your Answer

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