Web Analytics Made Easy -
StatCounter Variables in onClick commands - CodingForum

Announcement

Collapse
No announcement yet.

Variables in onClick commands

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Variables in onClick commands

    I've been trying some stuff out and I came accross a problem. I want to use a variable that will replace an object's name in a onClick command on a button. The variable is text entered in a text filed. How would I go about doing this?
    here is my script:
    Code:
    <script>
    var gordon = new hlguy("Gordon", "100", "100", "AR");
    
    function hlguy(name, health, armor, weapon)
    {
    this.name=name;
    this.health=health;
    this.armor= armor;
    this.weapon=weapon;
    
    this.display = display;
    this.hurt1hp= hurt1hp;
    this.hurt2hp= hurt2hp;
    this.hurt3hp= hurt3hp;
    
    }
    function createnewhlguy()
    {
    	newguystring= " ";
    	var thenewname = document.hl.playername.value
    	var createit = new hlguy(thenewname, "100", "100", "AR");
    	var thenewname = createit
    	newguystring = "Name: " + thenewname.name + " Health: " + thenewname.health;
    	document.hl.status.value = newguystring
    
    }
    
    
    function hurt1hp(namee)
    {
    	this.health = this.health - 1
    	
    	return namee.health
    }
    
    function hurt2hp()
    {
    	this.health = this.health - 2
    	
    	
    	this.display()
    }
    
    function hurt3hp()
    {
    	this.health = this.health - 3
    	
    	
    	this.display()
    }
    
    
    
    function display()
    {
    	var the_string = "";
    	var min_health = 0
    	if (this.health < min_health)
    	{
    		the_string = this.name + " IS DEAD!";
    	} else {
    		
    		the_string +=	"Name: " + this.name + "  Health: " + this.health;
    	
    	}
    
    	window.document.hl.status.value = the_string;
    }
    
    	
    var nametext = document.hl.playername.value
    
    
    </script>
    
    <form name="hl">
    Enter the Player's Name: 	<input type="text"  name="playername" size="30" value="Name"><br>
    Status of the Player:		<input type="text" name="status" size="50" ><br>
    <input type="button" value="Create the new Player" onClick="createnewhlguy();"><br>
    <input type="button" value="-1 HP" onClick="hurt1hp(nametext);"><br>
    <input type="button" value="-2 HP" onClick="nametext.hurt2hp();"><br>
    <input type="button" value="-3 HP" onClick="nametext.hurt3hp();"><br>
    
    
    </form>
    <script>
    </script>
    LogoJive.com - Professional Grade Web Development!
    My Domain Listing: dnListing.com/listing/Unknown

  • #2
    At a first glance, you mixted string with numbers.

    1. Some of your parameters are passed as string

    var gordon = new hlguy("Gordon", "100", "100", "AR");

    and later use as numbers

    this.health = this.health - 1
    ....

    Try parse the strings into integers:

    this.health=parseInt(health);
    this.armor= parseInt(armor);

    Further more, don't forget that the form's elements' values are string, also, so if you intend to use that values for a math operation, parse them too before use a math operator
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      I would suggest you just name you player object player, renaming objects sound to me something that should be avoided even if possible which I doubt it is.
      And you should put your functions within the object contructor so they can't be accessed directly if you want a true object.

      Comment

      Working...
      X