Aviel Fedida
Aviel Fedida

Reputation: 4102

Javascript, target.id and event.srcElement.id

i am trying to understand part of javascript code is refer to the code is:Link, now there is 2 more css files the first contain(sansStyle.css):

body, p, td, ol, ul, select, span, div, input {
    font: .9em/1.1em verdana, geneva, arial, helvetica, sans-serif;
}

the second contain(serifStyle.css):

  body, p, td, ol, ul, select, span, div, input {
        font: 1.1em/1.2em Times New Roman, Times, serif;
    }

my problem is only at a small part at the javascript code that i dont understand, if you take a look at the link and at the javascript part the is:

if (inVal) {
        if (typeof inVal == "string") {
            title = inVal;
        }
        else {
            title = inVal.target.id;
        }
    }
    else {
        title = window.event.srcElement.id;
    }

now i do understand that we refer at the else to the id becouse it got the same value as the title value we need, but the problem is that if its not a string so what is it? and how does it get for example inVal.target.id what is inVal.target is?, and about the window event.srcElement.id what event is it? if can some one please help me understand thoes two i will be very thankful.

Upvotes: 0

Views: 5147

Answers (2)

yesh
yesh

Reputation: 2070

inVal can be two things it can be a String or it can be a click event.

1.) inVal can be a string when it gets the title (which is the style I assume) see this line setActiveStylesheet(title)

If you look closely title will always be a string either form the cookie var thisCookie = cookieVal("style"); or from the getActiveStylesheet() function

2.) inVal can be a click event as Haynar pointed it out looks at

allButtons[i].onclick = setActiveStylesheet;

Hence you check if it is a string

    if (typeof inVal == "string") {
        title = inVal; // when it comes form the cookie or getActiveStylesheet()
    }
    else {
        title = inVal.target.id; // it is an event
    }

window.event is the event property. For example window.event.type will give you the type of the event that took place. Like wise window.event.srcElement or window.event.target both properties return the HTML element the event took place on. window.event.srcElement.id is going to return the id of the event.

Upvotes: 1

haynar
haynar

Reputation: 6030

inVal is the click event and the target is a DOM element on which that event has occured. If you look at this line:

allButtons[i].onclick = setActiveStylesheet;

you'll see that this function is being called when the button is clicked and the event is passed as argument to that function.

Upvotes: 2

Related Questions