473,372 Members | 814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Can you change the value of a variable with an OnClick?

Hi,

Javascript confuses me, so I usually limit myself to Dreamweaver's
built-in scripts for stuff like imageswaps. But this time I'm trying to
write something very simple myself. I do most of my stuff in ASP and PHP
so I'm familiar with server-side programming; for some reason JavaScript
syntax trips me up.

I want to assign a value to a variable according to an onclick event,
and then run an if...then on the variable, and according to the value
write some text to the page.

Here's my (relevant) code:

<!-- -- start -- -->

<head>
<SCRIPT type="text/Javascript">
var sketcharrow = 1;
</SCRIPT>
</head>

<!-- -- snip -- -->

<td><SCRIPT type="text/Javascript">
if (sketcharrow == 1) {
document.write("&laquo;");
}
</SCRIPT>
</td>

<td><SCRIPT type="text/Javascript">
if (sketcharrow == 2) {
document.write("&raquo;");
}
</SCRIPT>
</td>

<!-- -- snip -- -->

<A href="#" onClick="sketcharrow = 2;MM_swapImage
('logo','','/sketches/mixmatch/20050301.EQ.jpg',1);MM_setTextOfLayer
('logoinfo','','<h1>Logo: Spoke Equality</h1>')">Spoke Equality</A>

<!-- -- end -- -->

The page loads fine, with the text for the conditional sketcharrow == 1
writing as planned. But when I click on the link to change sketcharrow
== 2 the if...thens don't get the new value to work with, although the
other two onClick events execute perfectly.

I am a total novice with Javascript, so any illumination would be
greatly appreciated. Can anyone help? Is what I'm trying even possible?
Thanks.
Jul 23 '05 #1
3 5853
Byron wrote:
Hi,

Javascript confuses me, so I usually limit myself to Dreamweaver's
built-in scripts for stuff like imageswaps. But this time I'm trying to
write something very simple myself. I do most of my stuff in ASP and PHP
so I'm familiar with server-side programming; for some reason JavaScript
syntax trips me up.

I want to assign a value to a variable according to an onclick event,
and then run an if...then on the variable, and according to the value
write some text to the page.

Here's my (relevant) code:

<!-- -- start -- -->

<head>
<SCRIPT type="text/Javascript">
var sketcharrow = 1;
</SCRIPT>
</head>

<!-- -- snip -- -->

<td><SCRIPT type="text/Javascript">
if (sketcharrow == 1) {
document.write("&laquo;");
}
</SCRIPT>
</td>

<td><SCRIPT type="text/Javascript">
if (sketcharrow == 2) {
document.write("&raquo;");
}
</SCRIPT>
</td>

<!-- -- snip -- -->

<A href="#" onClick="sketcharrow = 2;MM_swapImage
('logo','','/sketches/mixmatch/20050301.EQ.jpg',1);MM_setTextOfLayer
('logoinfo','','<h1>Logo: Spoke Equality</h1>')">Spoke Equality</A>

<!-- -- end -- -->

[snip]
You're right, this approach is flawed (calling document.write() after
the page is loaded).

A simple function may suffice
function changeArrow(num){
document.getElementById.innerHMTL=num==2?"<":">";
}

<A href="#" onclick="changeArrow(2);... ">
Mick


Jul 23 '05 #2
Mick White wrote:
[snip]
You're right, this approach is flawed (calling document.write() after
the page is loaded).

A simple function may suffice
function changeArrow(num){
document.getElementById.innerHMTL=num==2?"<":">";
}
Typo:

document.getElementById("elementIDHere").innerHMTL =num==2?"<":">";
Mick

<A href="#" onclick="changeArrow(2);... ">
Mick

Jul 23 '05 #3
Byron wrote:
Hi,

Javascript confuses me, so I usually limit myself to Dreamweaver's
built-in scripts for stuff like imageswaps. But this time I'm trying to
write something very simple myself. I do most of my stuff in ASP and PHP
so I'm familiar with server-side programming; for some reason JavaScript
syntax trips me up.

I want to assign a value to a variable according to an onclick event,
and then run an if...then on the variable, and according to the value
write some text to the page.

Here's my (relevant) code:

<!-- -- start -- -->

<head>
<SCRIPT type="text/Javascript">
var sketcharrow = 1;
</SCRIPT>
</head>
You create a global variable here but I don't think it's needed.

<!-- -- snip -- -->

<td><SCRIPT type="text/Javascript">
if (sketcharrow == 1) {
document.write("&laquo;");
}
</SCRIPT>
</td>

Since the initial value of sketcharrow is 1, why not just put
"&laquo;" in as HTML? That is effectively all this script does.

<td id="td01">&laquo;</td>
<td><SCRIPT type="text/Javascript">
if (sketcharrow == 2) {
document.write("&raquo;");
}
</SCRIPT>
</td>

Same here, just set the content to "&nbsp;" and forget the
script.

<td id="td02">&nbsp;</td>

<!-- -- snip -- -->

<A href="#" onClick="sketcharrow = 2;MM_swapImage
('logo','','/sketches/mixmatch/20050301.EQ.jpg',1);MM_setTextOfLayer
('logoinfo','','<h1>Logo: Spoke Equality</h1>')">Spoke Equality</A>


Simply changing the value of sketcharrow will not cause anything
to happen unless you have created some event to monitor the
value and change things if sketcharrow changes... but I think
such an approach is not necessary here.

Using href="#" will cause most browsers to scroll to the top of
the page if you don't cancel the navigation - the final
statement of the in the onclick event should be "return false;".

To make the content of the td's change, you have to actually
write new content to them. Give them an ID, then use
getElementById or similar, then change their content.

Better code layout will really help too. Sample code below, I
make no guarantees on the MM_ functions but changeArrow works in
Firefox and IE. I have modified the function so you pass
element IDs and the content to put in them as pairs, e.g.

changeArrow('td01','&nbsp;','td02','&raquo;');

will change the content of 'td01' to '&nbsp;' and 'td02' to
'&raquo;'. You can pass as many id/content pairs as you like,
as long as they match.
<script type="text/javascript">
function changeArrow(a,b) {

// Make allowances for old IE (courtesy Dr J Stockton)
if (document.all && !document.getElementById) {
document.getElementById = function(id) {
return document.all[id]
}
}

// Go through all the arguments, change elements as we go
var x;
for (var i=0, aLen=arguments.length; i<aLen; i++) {
if (x = document.getElementById(arguments[i])){
x.innerHTML = arguments[++i];
}
}
}
</script>

<table><tr>
<td id="td01">&laquo;</td>
<td id="td02">&nbsp;</td>
</tr></table>

<A href="#" onClick="
changeArrow('td01','&nbsp;','td02','&raquo;');
MM_swapImage('logo','',
'/sketches/mixmatch/20050301.EQ.jpg',1);
MM_setTextOfLayer('logoinfo','',
'<h1>Logo: Spoke Equality</h1>');
return false;
">Spoke Equality</A>
--
Rob
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: monika | last post by:
hi ... I have an asp page which has 3 buttons. <p align="center"><input class="button" type="button" onClick="location='welStudent.asp';" value="Click to write a new story"></p> <p...
8
by: KS | last post by:
Just to show some code to show the consept. <img id="date" onclick="javascript:show_calendar();" src="/PlexSysWeb/images/show-calendar.gif" width=20 height=18 border=0> What i want the...
4
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an...
2
by: Ant | last post by:
Ok, so I have a regular input textbox on my webpage and I'm trying to work out how to make it so when the user presses a button the the textbox holds the value of a variable. I just can't work out...
1
by: Josema | last post by:
Hi to all, I have this javascript code inside my aspx page, but i would like to change the value of the variable lunch depending if i click a button in my form. i dont know if its possible. ...
10
by: IchBin | last post by:
I am trying to set the state of a radio button. I do not see what I am doing wrong. Sorry, I am new at this.. I need another set of eyes to look at this snip of code. I am trying to set the radio...
21
by: ndeeley | last post by:
Hi, I have created several queries which produce the same data based on a slightly difference set of variables: <cfquery name="KWBand1S" dbtype="query"> select ClientFK,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.