-4

I have piece of Javascript code.

function checkProductId(value){
        checkboxid = value;
        // First create an event
        var click_ev = document.createEvent("MouseEvent");
        // initialize the event
        click_ev.initEvent("click", true, true);

        if(document.getElementById(value).checked){
            // trigger the evevnt
            document.getElementById("addproduct").dispatchEvent(click_ev);
        } else {
            document.getElementById("removeproductdata").dispatchEvent(click_ev);
        }
    }

When I am load my page in safari browser its give me error:

'null' is not an object (evaluating 'document.getElementById(value).checked')

And document.getElementById(value).checked its not working.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Prashant Parekh
  • 428
  • 1
  • 6
  • 27

2 Answers2

1

Could you try

document.getElementById(value).getAttribute('checked)

Maybe there is something wrong with your HTML?

Does

document.getElementById(value)

it return something more then null value?

speedingdeer
  • 1,236
  • 2
  • 16
  • 26
1

The problem is that document.getElementById(value) is returning null. Make sure that the value you are passing corresponds to the id of an element on the page.

If this isn't helpful, please add the html and the code that calls the function to your question.

rjmunro
  • 27,203
  • 20
  • 110
  • 132
  • i am passing value as an id of element, and its not a passing null. – Prashant Parekh Jun 11 '13 at 10:18
  • @PrashantParekh You are passing a string, fir example "foo", and you need a corresponding ``. You don't have one, so `document.getElementById("foo")` is returning null. – rjmunro Jun 11 '13 at 12:17