Ash
Ash

Reputation: 237

Checking for the match of the Object name

Here I want to check the match for the particular class name (last class with the name of YtoD) and write a condition according to it. I have not Idea why it is not working.

HTML:

 Select Range: <a href="#" class="x_lab" name="month" onclick="Lab(this)">Last Month </a>|<a href="#" class="x_lab" name="2 weeks" onclick="Lab(this)" class="label2"> Last 2 Weeks </a>|<a href="#" class="x_lab" name="1 week" onclick="Lab(this)" class="label2"> Last 1 Week</a>|<a href="#" class="x_lab" name="YtoD" onclick="Lab(this)"> Year to Date</a>

Javascript

function Lab(obj) {
    var a = obj.name;
    if (a != YtoD) {
        document.getElementById("linechartxaxislabel").innerHTML = "Price     
    Timeline" + "(Last" + a + ")";
    } else {
        document.getElementById("linechartxaxislabel").innerHTML = "Year     
    to Date";
    }
}

What should I do here?

Upvotes: 0

Views: 68

Answers (1)

ivarni
ivarni

Reputation: 17878

Try this

function Lab(obj) {
    var a = obj.getAttribute('name');
    if (a != 'YtoD') {
        document.getElementById("linechartxaxislabel").innerHTML = "Price Timeline" + "(Last" + a + ")";
    } else {
        document.getElementById("linechartxaxislabel").innerHTML = "Year to Date";
    }
 }

Firstly, the object you get in does not have a javascript attribute called name, as it's a DOM element. You need to access attributes using the DOM API (https://developer.mozilla.org/en-US/docs/Web/API/element.getAttribute)

Second, once you have the name it's a String so you need to compare it with the String YtoD and not an object reference.

Upvotes: 2

Related Questions