1

Simple task here, but I can't get it right: If a table-cell is clicked, I need to check whether it contains an input field or not. If it's not there, a new one should be created.

So far I got this:

$("tbody td").bind("click", function(){
  $this = $(this);
  var newInput = $(document.createElement("input")).attr("class", "input-small");
  $this.append(newInput);
})

This works, but as you can see, it misses the test if an input is already there. I already tried various methods including if($this.text.length){...}, if($this.val().hasClass("input-small") == true){...} but they all fail. So how do I do it right? Whats the right way to check if the clicked cell contains an input field?

4 Answers 4

11

Something like the following will work

if ($this.find('input').length) { 
    // the td clicked contains an <input>
}

$this is a jQuery object that wraps the current <td> element (referenced by this), so we need to look into this element in the DOM to see if it contains an <input> element. If it does, the .length property will be greater than 0 and thus a truthy value.

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

Comments

1
if ($(this).children("input").length == 0) //the cell doesn't contain any input elements.

Comments

1

Russ Cam has the answer for you (upvote him!), I just wanted to help you optimize the code some what.

// .click just points to jquerys .on function, so calling it directly is faster.
$('tbody td').on('click', function() {
     // always declare your variables properly, some javascript engines could have issues, however excellent that you are caching $(this)
     var $this = $(this),
         newInput = $('<input class="input-small"');

     // .lenght will return false if the length is 0, so no need to compare it to an int
     if(!$this.find('input').length) {
         newInput.appendTo($this);
     }
});

edit: fixed logic

3 Comments

Thanks for the commented code! After fixing newInput to $('<input type="text" class="input-small">'); it nearly worked. Problem is I had to do it like if($this.find('input').length == false) because otherwise it did not work. Any reason for that? (Because what you've written sounds logical to me!)
Seems very strange, sure there isn't a typo somewhere? I made a fidde here jsfiddle.net/LxHK9 , where you can see it in action. Oh nevermind, its the logic in my code that's wrong, it should be if(!$this.find('input').length) as it is now it checks if its there, and then appends it. Not the other way around.
haha yeah, seems to be a logical error on my side! if($this.find('input').length){...} - this means an input is there. So if I put newInput.appendTo($this); it's basically senseless for my purpose. But with if-else it works like a charm! :)
0

Try:

if($(this).find('input').length > 0){

    var newInput = $(document.createElement("input")).attr("class", "input-small");
      $this.append(newInput);
}

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.