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-09-2013, 08:49 PM   PM User | #1
JS Newbie
New Coder

 
Join Date: Dec 2012
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
JS Newbie is an unknown quantity at this point
Change text on page based on id??

Hi,

I'm trying to add a some text to my site that changes depending on the element id but i think the JS function is incorrect.

Code:
<script language="javascript">

function modetext() 
{
i = document.getElementById('datamem14').value;
if (i == 1) 
{
document.getElementById('mode') = "Auto"; // not sure if this should have a .txt?? i.e document.getElementById('mode').txt = "Auto";???
}
else if (i == 2) 
{
document.getElementById('mode') = "Manual"; 
}
else
{
document.getElementById('mode') = "Off"; 
}
}

</script>
and the HTML bit...

Code:
<!-- is this ok???? -->

<input id="mode" class="smalltextbox1"  style="position:absolute; left: 0px; top: 0px;" name="" type="text" value="WAIT" >

<!-- or should this be below?? -->

<h1 id="mode" "></h1>

<!-- i think this is ok -->

<input id="datamem14" class="smalltextbox"  style="position:absolute; left: 0px; top: 25px;" name="" type="text" value="WAIT" >
any ideas appreciated!
JS Newbie is offline   Reply With Quote
Old 01-09-2013, 09:18 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 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
If you use
Code:
<input id="mode" ...>
Then you need
Code:
document.getElementById('mode').value = "Manual";
On the other hand, if you use
Code:
<h1 id="mode" "></h1>
then you need
Code:
document.getElementById('mode').innerHTML = "Manual";
__________________
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-09-2013 at 09:21 PM..
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
JS Newbie (01-11-2013)
Old 01-09-2013, 09:26 PM   PM User | #3
JS Newbie
New Coder

 
Join Date: Dec 2012
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
JS Newbie is an unknown quantity at this point
Brilliant. Thank you very much

I'll update my stuff and report back!
JS Newbie is offline   Reply With Quote
Old 01-09-2013, 10:12 PM   PM User | #4
JS Newbie
New Coder

 
Join Date: Dec 2012
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
JS Newbie is an unknown quantity at this point
All working , Again many thanks.
JS Newbie is offline   Reply With Quote
Old 01-11-2013, 11:08 PM   PM User | #5
JS Newbie
New Coder

 
Join Date: Dec 2012
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
JS Newbie is an unknown quantity at this point
And a wee bit more help required ;-)

Evening All,

Just to go one step further on this.....

I'm looking to expand this to to maybe 100 to 150 different phrases based in the datamem14 value and was wondering if I can do this either via a lookup table, external .txt file or 'For to Loop'??

Any recommendations appreciated. .

Code:
<script language="javascript">

function modetext() 
{
i = document.getElementById('datamem14').value;
if (i == 1) 
{
document.getElementById('mode').value = "Auto"; 
}
else if (i == 2) 
{
document.getElementById('mode').value = "Manual"; 
}
else
{
document.getElementById('mode').value = "Off"; 
}
}

</script>
JS Newbie is offline   Reply With Quote
Old 01-11-2013, 11:32 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
An array would probably be easiest as then you'd only need a single statement to do the assignment.

Code:
<script type="text/javascript">

var vals = ["Off","Auto","Manual"];
function modetext() 
{
i = document.getElementById('datamem14').value;
if (i < 0 || i >= vals.length) i = 0;
document.getElementById('mode').value = vals[i]; 
}
</script>
Just add as many entries to the array as you need, the rest of the code will not need to be changed.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
JS Newbie (01-12-2013)
Old 01-11-2013, 11:39 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 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
Lookup table is easiest. External file may be best. You have to decide that part.

Lookup table is trivial:
Code:
var phrases = [
     null, /* 0 is never used */
     "this is phrase 1",
     "this is phrase 2",
     ..
     "this is phrase 140"
];
var chosenPhrase = phrases[document.getElementById('datamem14').value];
if ( chosenPhrase == null ) chosenPhrase = "That is not a valid selection";
As an alternative, if you wanted the value from datamem14 to be something other than a number form 1 to 140:
Code:
var phrases = {
    "auto" : "This is automatic",
    "manual" : "5 speed on the floor",
    "chain" : "Must be a bicycle",
    ...
}
var chosenPhrase = phrases[document.getElementById('datamem14').value];
if ( chosenPhrase == null ) chosenPhrase = "That is not a valid selection";
__________________
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 offline   Reply With Quote
Users who have thanked Old Pedant for this post:
JS Newbie (01-11-2013)
Old 01-11-2013, 11:41 PM   PM User | #8
JS Newbie
New Coder

 
Join Date: Dec 2012
Posts: 13
Thanks: 6
Thanked 0 Times in 0 Posts
JS Newbie is an unknown quantity at this point
That's flipping great. Thanks!
JS Newbie 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 12:08 PM.


Advertisement
Log in to turn off these ads.