Welcome to WebmasterWorld Guest from 23.22.200.6

Forum Moderators: open

Message Too Old, No Replies

OnClick Issue

     
3:56 am on Aug 5, 2012 (gmt 0)

New User

joined:Apr 9, 2012
posts: 17
votes: 0


Ok, as I am new to Java script please forgive me if this issue is an easy one and Im just missing something I cannot for the life od me figure out.

I am trying to toggle the visibility of a div based on the click of a radio button.
<input type="radio" name="spell_type" value="dpsheal" onClick="toggleVisibility('dpsheal');" /> DPS/Heal
<input type="radio" name="spell_type" value="buff" onClick="toggleVisibility1('buff');" /> Buff


And the function looks like this:
function toggleVisibility(divid) {
if (divid="dpsheal"){
document.getElementById("dpsheal").style.visibility = "visible";
document.getElementById("buff").style.visibility = "hidden";
}
else if (divid="buff")
{
document.getElementById("buff").style.visibility = "visible";
document.getElementById("dpsheal").style.visibility = "hidden";
}
}


But it just won't work. Any input would be greatly appreciated! Thanks!
4:36 am on Aug 5, 2012 (gmt 0)

Full Member

5+ Year Member

joined:Mar 4, 2008
posts:210
votes: 0


Your problem is that the comparison should be if (divid=="dpsheal") ...note: == is a comparison, = is an assignment. Also, you're using getElementById, but you don't have an id attribute for the input elements.

That written, I'd do your code like this:

<input type="radio" name="spell_type" value="dpsheal" onClick="toggleVisibility(this);" /> DPS/Heal
<input type="radio" name="spell_type" value="buff" onClick="toggleVisibility(this);" /> Buff

<script type="text/javascript">
function toggleVisibility(element)
{
var isVisible = (element.style.visibility == "visible");
if (isVisible) element.style.visibility = "hidden";
else element.style.visibility = "visible";
}
</script>
4:44 am on Aug 5, 2012 (gmt 0)

New User

joined:Apr 9, 2012
posts: 17
votes: 0


Forgive me I forgot to put that in.

<div class="hide" id="dpsheal">
Min Damage:&nbsp;<input type="text" name="dps_min" size="5" maxlength="6" onkeypress="return onlyNumbers();">
</input>&nbsp;&nbsp;&nbsp;Max Damage:&nbsp;
<input type="text"name="dps_max" size="5" maxlength="6" onKeyPress="return numbersonly(event, true)">
</div>
5:31 pm on Aug 6, 2012 (gmt 0)

New User

joined:Apr 9, 2012
posts: 17
votes: 0


And after adding your code and clicking on the radio buttons, nothing happened still. Not sure what I am doing wrong.
8:53 pm on Aug 6, 2012 (gmt 0)

New User

joined:Apr 9, 2012
posts: 17
votes: 0


Actually I have found another way to do what I need done. Thank you for your time :)
9:32 am on Aug 7, 2012 (gmt 0)

New User

joined:Aug 3, 2012
posts: 4
votes: 0


Just if you didn't know about it: jQuery.

This JS libary makes these functions very easy to use. For example:

$('#dpsheal').click(function(){
$('#dpsheal').toggle();
});

That would have the same effect as your JavaScript.

[api.jquery.com...]
9:40 am on Mar 4, 2013 (gmt 0)

Junior Member

joined:Mar 1, 2013
posts: 55
votes: 0


Please try the below code for the same.

JS:
<script>
$(document).ready(function(){
$("input:first").click(function(){
$("input:last").toggle();
$("span:last").toggle();
});
$("input:last").click(function(){
$("input:first").toggle();
$("span:first").toggle();
});
});
</script>

HTML:
<input type="radio" name="spell_type" value="dpsheal" /><span>DPS/Heal</span>
<input type="radio" name="spell_type" value="buff" /> <span>Buff </span>
1:39 pm on Mar 4, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 23, 2005
posts:742
votes: 0


Just if you didn't know about it: jQuery.

This JS libary makes these functions very easy to use.

I see no reason for him to include a 40K library just to do those simple javascript functions.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members