Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-15-2013, 08:47 PM   PM User | #1
LionFische
New to the CF scene

 
Join Date: Dec 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
LionFische is an unknown quantity at this point
innerHTML basic question

Hi, I have the following problem. I have a text box, I want to be able to press a button to increment the contents and then update the text box.

I have the following code.

HTML
[CODE]
<fieldset>
<legend>Details</legend>

<input id="textBox" type="text" value="0">

<input id = "test" type = "submit" value = "+"/>

</fieldset>

[CODE]

JS
[CODE]

window.onload = function (){

test.onclick = function(){

var x = document.getElementById("textBox").value;

x++;

document.getElementById("textBox").innerHTML = x;

//alert(x);
};

};
[CODE]

Quite basic, but I'm not getting it to work. Any advice would be much appreciated.
LionFische is offline   Reply With Quote
Old 01-15-2013, 08:56 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,562
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Your basic problem is that you are using a SUBMIT button!

Because you are not suppressing the natural action of the submit button, after you do the increment, the submit button continues on to do its natural action...which is to SUBMIT THE <FORM>! Which means that the browser sends the form information *BACK* to the server and the server RESENDS the page back to the browser. And you have accomplished nothing.

So to start with, change from type="submit" to simply type="button"

Then you have two mistakes in the JS code:
Code:
test.onclick = function()
{
    var x = Number(document.getElementById("textBox").value); // ensure you get a number!
    x++;
    // put updated number *BACK* in the value!
    // text fields do NOT HAVE any innerHTML property!
    document.getElementById("textBox").value = x;
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-15-2013 at 09:01 PM..
Old Pedant is online now   Reply With Quote
Old 01-15-2013, 08:57 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,562
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
By the by, for maximum compatibility with all browsers, change
Code:
test.onclick = function()
to
Code:
document.getElementById("text").onclick = function()
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 01-15-2013, 09:02 PM   PM User | #4
LionFische
New to the CF scene

 
Join Date: Dec 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
LionFische is an unknown quantity at this point
Thank you so much.

LF.
LionFische is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:21 PM.


Advertisement
Log in to turn off these ads.