0

I am editing a website in Drupal 7, and using a custom JS file to add a bit of scripting functionality to a form that I created with the Webform module.

Basically, there is a checkbox that, if checked, will prevent the user from having to fill in redundant fields on the form if the information from one section also applies to the other section.

This is my code, for now I am just testing to see if the information gets passed between the functions.

$(document).ready(function() {
    var checkbox = $("#edit-submitted-contact-information-pi-checkbox-1");
    checkbox.onchange = "checkPI(this);";
});

function checkPI(x) {
    if(x.checked) alert("Checkbox is checked");
    else alert("Checkbox is unchecked.");
}

When I click the checkbox, however, nothing happens. I double checked and the JS script is being added to the page by Drupal (in the .info file I added scripts[] = myjs.js and also linked jQuery before that.

Upon doing other alert() tests, I found that it is finding the check() function from the document ready handler.

Am I missing something/not doing something a certain way?

Also note: I have to make sure this site works in IE 8, so I cannot use the setAttribute() method to add the onchange listener.

2
  • you can use setAttribute in IE8. here's how: stackoverflow.com/questions/15329292/… Commented Jul 25, 2014 at 17:25
  • OK, but do I have to use setAttribute to get this working at all? Commented Jul 25, 2014 at 17:28

1 Answer 1

-1

Found the solution...

The problem was that jQuery was treating the checkbox element as [object Object] instead of [object HTMLInputElement]. Therefore, to avoid the hassle, I just used plain old JavaScript to assign the functions and listeners and everything works now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.