1

Following is the HTML structure I'm using

<div id="divid">
   <table>
     <tr>
       <td onclick="var ele = document.getElementById('inputID'); ele.click();">
          <input id="inputID" onclick="event.stopPropagation();"  name="nameOfElement" type="checkbox"/>
       </td>
     </tr>
    </table>
</div>

I'm trying to set a click event on element using JQuery as follows:( for other functionality elsewhere in page)

 $("#inputID").parent().on('click',function(){ // call to other functions})

this attachment leads to following error:

  Uncaught Error: Syntax error, unrecognized expression: )

I have to give following functionality:

  1. If user click on td then input element must be checked and if clicked on then click event must not be propagated to parent i.e. td as that will reverse the effect.

  2. Also I want to Jquery's click event to execute.

Currently, only first functionality is working and event.stopPropagation() is stoping the second one.

how can I achieve both of them?

please have a look at http://jsfiddle.net/zqg0Lgbh/4/ for demo of issue.

7
  • 2
    I don't understand the question. And it's been a very long time since live deprecation (and removal). Commented Dec 16, 2014 at 8:36
  • api.jquery.com/live live() is long deprecated, check for yourself Commented Dec 16, 2014 at 8:37
  • okay I will replace it with On() Commented Dec 16, 2014 at 8:38
  • @dystroy I edited Question and include a demo on fiddle, have a look now Commented Dec 16, 2014 at 9:35
  • @ArvindBishnoi What's not working right in your demo ? Commented Dec 16, 2014 at 9:38

4 Answers 4

3

Use on() instead of live() since live() is long deprecated

$("#inputID").parent().on('click',function(){

Also, it's not a good idea to use

<td onclick="var ele = document.getElementById('inputID'); ele.click();"

Instead create on click event in JS like shown above. (and add class or id to your td element so you can attacht click event to it ofcourse). If you must call onclick from HTML, then perhaps try

onclick="javascript:var ele = document.getElementById('inputID'); ele.click();"

That sometimes helps if you are not sure the code is being executed. Also try to alert() something to make sure the code actually executes.

Also you have a syntax error in your code. (onlcick instead of onclick)

<input id="inputID" onlcick="event.stopPropagation();"
Sign up to request clarification or add additional context in comments.

7 Comments

Call to click event never comes!
that Jquery click event which I'm attaching to parent of input element for controlling show/hide other elements in page.
I did tried to put alert() there but I got error (please look in question Itself).
The event that you add to parent inline, or the one that is added in JS?
Can anyone tell me from where that extra ")" comes into picture??
|
0

First, I would remove all those ugly inline event handlers.

Then, I would use jQuery to ensure a click on a cell containing a radio button checks that radio button :

$('td:has(>input[type=radio])').click(function(){
   $('input[type=radio]', this).prop('checked', true);
});

This way you don't have to use stopPropagation.

Demonstration (note that I fixed a few other things, like using fixing the missing name in the function declaration or importing jQuery.

Asides:

  • you really should stop using inline styles and inline event handlers. This code isn't maintainable.
  • changing the behavior of widgets and their surroundings isn't really a best practice as it surprises the user and leads to various ergonomics problem

2 Comments

i totally agree with you, but this decision is not mine. That Inline code is legacy I got and no one is willing change approach.
Then just remove all those event handlers just before your code : $('.Pointer').each(function(){ this.onclick=null });
0

Remove inline handler

$("#inputID").click(function () {

    if (this.checked) {
        alert("call other function")
    }

});

6 Comments

If I remove inline handler then what about First functionality. Also Call to click event never comes!
why you call click event , you can call this other function inside this checked
I have to some show/hide thing elsewhere in page which depends upon this answer.
Then you can use your function depends on this.checked
I'm not able to follow, can you please elaborate?
|
0

This works:

$("#inputID").parent().on("click", function(){
    alert("Trigger other events");
});

Here's a fiddle

http://jsfiddle.net/vkewuu6L/

If you look at the fiddle the border represents the parent of "#inputID". You can click on the border to trigger the alert.

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.