Ben Clarke
Ben Clarke

Reputation: 256

Javascript function not finding ID

I am trying to code something in Javascript that will call a function on PageLoad that will find the height of a Calender_Control. If the height is equal to 398px then set the height of a text area to have 4 rows, else set the rows to 6.

This is what I have tried so far and it isn't finding the ID.

        function changeHeight() {
        var height = document.getElementById("#calender_control");
        if (height == 398) {
            document.getElementById("#calender_control").rows = "4";
        }
        else {
            document.getElementById("#calender_control").rows = "6";
        }
    }

Why isn't my piece of code working?

Upvotes: 0

Views: 72

Answers (4)

Hiren Rajpura
Hiren Rajpura

Reputation: 1

Try below code...

 function changeHeight() {
    var height = document.getElementById("calender_control").height;
    if (height == 398) {
        document.getElementById("calender_control").rows = "4";
    }
    else {
        document.getElementById("calender_control").rows = "6";
    }}

Upvotes: 0

Md Nazmoon Noor
Md Nazmoon Noor

Reputation: 3307

If you want to use jQuery.

function changeHeight() {
    var height = $("#calender_control").height();
    if (height == 398) {
        $("#calender_control").attr("rows", "4");
    }
    else {
        $("#calender_control").attr("rows", "6");
    }
}

Upvotes: 0

Jamiec
Jamiec

Reputation: 136249

document.getElementById does not use a hash to denote an id.

But that's just the first problem, you're expecting to get a number from a call to getElementById:

var height = document.getElementById("#calender_control");
if (height == 398) { ...

But the call to getElementById returns an HTMLDomElement

Upvotes: 0

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62498

you have to do using javascript for selecting element by id:

document.getElementById("calender_control")

but if you want with jquery then:

$("#calender_control")

$("#calender_control") is equivalent to document.getElementById("calender_control")

Upvotes: 4

Related Questions