/    Sign up×
Community /Pin to ProfileBookmark

Question about ‘this’ in accessing elements …

Question about ‘this’ as a reference. 😮

If I have in the <body> section …

[code]
<div id=”TEXT2″ class=”TEXT”>
<span class=”RB” onclick=”pick(this)”>Calculators</span>
<span class=”RB” onclick=”pick(this)”>Algerbraic</span>
<span class=”RB” onclick=”pick(this)”>RPN</span>
</div>
[/code]

And I have the function ‘pick(this)’ defined in the <head><script> area …

[code]
function pick(which) {
alert(which.innerHTML);
// which.className = (which.className == ‘RB’) ? “RBON” : “RB”;
}
[/code]

Finally, the CSS applicable statements are …

[code]
input.RB { border: 1px solid black;
background-color: #e8d8c8;
color: black;
width: 100px;
}
input.RB:hover { background-color: orange; }

input.RBON { border: 1px solid blue;
background-color: lightgreen;
color: blue;
width: 100px;
}
input.RBON:hover { background-color: orange; }
[/code]

The ‘which’ passed in the ‘pick()’ function correctly displays the .innerHTML
BUT (big but) ?
it does not seem to have any effect on the ‘className’ I’m trying to change in the // commented out line of the ‘pick()’ function.

How do I determine which ‘which’ (from the ‘this’ passed to the function,
to apply to change the className clicked upon?

I’ve also tried to use a parent.id to choose the correct <span> tag to alter,
but this too failed to have any effect or gave errors that it was undefined or undetermined. Obviously my DOM skills are sore-ly lacking. 😮

Any clarification to my understanding is much appreciated. ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meDec 17.2009 — there are no input.RBON, and .className changes seems to be invisible to firebug's html tab...

tested in chrome3:

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type='text/css'>
.RB { border: 1px solid black;
background-color: #e8d8c8;
color: black;
width: 100px;
}
.RB:hover { background-color: orange; }

.RBON { border: 1px solid blue;
background-color: lightgreen;
color: blue;
width: 100px;
}
.RBON:hover { background-color: orange; }

</style>
</head>
<body>
<div id="TEXT2" class="TEXT">
<span class="RB" onclick="pick(this)">Calculators</span>

<span class="RB" onclick="pick(this)">Algerbraic</span>

<span class="RB" onclick="pick(this)">RPN</span>
</div>

<script type='text/javascript'>
function pick(which) {
alert(which.innerHTML);
which.className = (which.className == 'RB') ? "RBON" : "RB";
}
</script>
</body>
</html>

[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorDec 17.2009 — there are no input.RBON, and .className changes seems to be invisible to firebug's html tab...

tested in chrome3: ...

[/QUOTE]


I apologize 'rnd me'. :o Your code works fine.

I had intended the input.RB and input.RBON to be span.RB and span.RBON.

However, after using your code, I found I needed neither the input. or the span. designations.

(My only excuse was that I posted the original after midnight and I may have suffered a brain infark while programming in the dark).

Let me demonstrate the full script I'm trying to develop and try again to verbalize my desire.
<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;title&gt;Query Tabs&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;

&lt;style type="text/css"&gt;
/* move CSS to external file later */

div.TEXT {
border: 1px solid blue;
background-color: #e8d8c8;
display: none;
cursor: pointer;
}

#tabContainer { width:900px; }
#tabContainer span {
border-left: 0px;
border-right: 1px solid blue;
padding-left:5px;
padding-right:5px;
width:100px;
}
#tabContainer span:hover { background-color:lightgreen; }

.RB {
border: 1px solid black;
background-color: #e8d8c8;
color: black;
width: 100px;
}
.RB:hover { background-color: orange; }

.RBON {
border: 1px solid blue;
background-color: lightgreen;
color: blue;
width: 100px;
}
.RBON:hover { background-color: orange; }
/* line 40 */
&lt;/style&gt;

&lt;script type="text/javascript"&gt;
// From posts by 'logic ali' : http://www.webdeveloper.com/forum/member.php?u=80312
// With help from 'rnd me' : http://www.webdeveloper.com/forum/showthread.php?t=221652

function MenuResponse(Q,R) {
answer = Q.match(/horizontal|vertical|tabs/i) ? Q : R;
if (answer != '') { alert(answer); } // else { alert('Invalid entry'); } // else part un-used
}
function SiteResponse(Q,URL) {
answer = Q.match(/Google|Bing|Yahoo|WD|CF|DIC/i) ? URL : ''; <br/>
if (answer != '') {
document.getElementById('frameInfo').src = answer;
document.getElementById('GoToSite').href = URL;
document.getElementById('GoToSpan').innerHTML = URL;
// } else { // alert('Invalid: '+URL); // unused alert
}
}

function choose(which) {
var mbtn = document.getElementsByTagName('input');
for ( var i=0; i&lt;mbtn.length; i++ ) {
var button = document.getElementById("RB"+i);
var text = document.getElementById("TEXT"+i);
button.className = (button == which) ? "RBON" : "RB";
text.style.display = (button == which) ? "block" : "none";
}
}
function pick(which) {
alert(which.innerHTML);
[COLOR="Magenta"]// somehow use document.getElementsByTagName('span') here like function above ???
[/COLOR] which.className = (which.className == 'RB') ? "RBON" : "RB"; // works as 'toggle' display (?)
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!-- use following 'onload=' to initialize display as full display --&gt;
&lt;body onload="choose(document.getElementById('RB0'))"&gt;
&lt;form name="myForm"&gt;

&lt;div id="tabContainer"&gt; &lt;!--style="position:relative; top:20px; left:20px" --&gt;

&lt;input class="RB" type=button id="RB0" value="Calendars" onclick="choose(this);"&gt;
&lt;input class="RB" type=button id="RB1" value="Calculators" onclick="choose(this);"&gt;
&lt;input class="RB" type=button id="RB2" value="Menus" onclick="choose(this);"&gt;
&lt;input class="RB" type=button id="RB3" value="Sites" onclick="choose(this);"&gt;
&lt;br /&gt;

&lt;div id="TEXT0" class="TEXT"&gt;
&lt;span class="RB" onclick="pick(this)"&gt;Calendars&lt;/span&gt; <br/>
&lt;span class="RB" onclick="pick(this)"&gt;Day &lt;/span&gt; <br/>
&lt;span class="RB" onclick="pick(this)"&gt;Month&lt;/span&gt;
&lt;span class="RB" onclick="pick(this)"&gt;Year&lt;/span&gt;
&lt;/div&gt;

&lt;div id="TEXT1" class="TEXT"&gt;
&lt;span class="RB" onclick="pick(this)"&gt;Calculators&lt;/span&gt; <br/>
&lt;span class="RB" onclick="pick(this)"&gt;Algerbraic&lt;/span&gt; <br/>
&lt;span class="RB" onclick="pick(this)"&gt;RPN&lt;/span&gt;
&lt;/div&gt;

&lt;div id="TEXT2" class="TEXT"&gt;Menus: &lt;!-- acts as a placeholder --&gt;
&lt;!-- &lt;span class="RB" onclick="MenuResponse('','')"&gt;Menus&lt;/span&gt; --&gt; &lt;!-- acts as do-nothing selection --&gt;
&lt;span class="RB" onclick="MenuResponse('horizontal','Horizontal')"&gt;Horizontal&lt;/span&gt; <br/>
&lt;span class="RB" onclick="MenuResponse('vertical','Vertical')"&gt;Vertical&lt;/span&gt;
&lt;span class="RB" onclick="MenuResponse('tabs','Tabs')"&gt;Tabs&lt;/span&gt;
&lt;/div&gt;

&lt;div id="TEXT3" class="TEXT"&gt;Sites: &lt;!-- acts as a placeholder --&gt;
&lt;!-- &lt;span class="RB" onclick="SiteResponse('','')"&gt;Sites: &lt;/span&gt; --&gt;&lt;!-- do-nothing selection --&gt;
&lt;span class="RB" onclick="SiteResponse('Google','http://www.google.com')"&gt;Google&lt;/span&gt; <br/>
&lt;span class="RB" onclick="SiteResponse('Bing','http://www.bing.com')"&gt;Bing&lt;/span&gt; <br/>
&lt;span class="RB" onclick="SiteResponse('Yahoo','http://www.yahoo.com')"&gt;Yahoo&lt;/span&gt; <br/>
&lt;span class="RB" onclick="SiteResponse('WD','http://www.webdeveloper.com')"&gt;Web Developer&lt;/span&gt; <br/>
&lt;span class="RB" onclick="SiteResponse('CF','http://www.codingforums.com')"&gt;Coding Forums&lt;/span&gt; <br/>
&lt;span class="RB" onclick="SiteResponse('DIC','http://www.dreamincode.net')"&gt;Dream In Code&lt;/span&gt; <br/>
&lt;/div&gt;

&lt;/div&gt;

&lt;iframe id="frameInfo" src="" style="height:450px;width:900px"&gt;&lt;/iframe&gt;
&lt;div&gt;&lt;a id="GoToSite" href="#" onclick="window.open(this.href)"&gt;&lt;span id="GoToSpan"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;


The script itself is not the final product, but will get me a long way towards my goal.

Here's my DILEMA ...

for the first 2 tab buttons which have a pick() function defined for the second row

(I do not have the pick() function implemented for the last 2 tab buttons.


I'll add them later if I can figure out the first 2 button logic):

ACTIONS ...

If you click on the top row selections, the second row changes to reflect new picks.

The :hover works fine, and the top row retains the color of the last tab selected.

If you move over the second row of picks, the :hover again works fine.

Then if you click on a selection (thanks to your suggestion) the tab color changes.

BUT, here's the QUESTION ...

It does not go back to the original background color if another selection is then made

unless you click the second row tab AGAIN.


It appears to be acting as a 'toggle' display here and will go back to original background color if clicked twice.

I want the second row of choices to act like the first row.


Hover color changes, but change background color when selected, until a different selection is made.

My THOUGHTS were that ...

1. I should be able to view the <span>s associated with the <div> parent for each of the 'TEXT0' ... 'TEXT3' sections.

2. I was trying to avoid assigning 'id=' values for each of the <spans> like I did in the first row tabs (id='RB0' ... id='RB3')

3. I don't know how to access a DOM referenced elements when I have only the <span> tag

and the only ID available is in the parent element.

As you can see I've used the getElementsByTagName() function, but I'm stumped beyond that.

Does my question make more sense now that I'm back in the light of day? ?

Thanks again for the earlier suggestion ... I didn't know that I could do it that way. ?
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...