2 Replies - 514 Views - Last Post: 28 November 2011 - 07:41 AM

#1 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • Joined: 16-September 09

Adding input to div on click with if statement

Posted 27 November 2011 - 11:02 PM

Hey guys!!

So I'm trying to add a textbox to my div -- the code to add does work but when I add in the if statement to make sure that there won't be multiple inputs, the code does not work...

This begs two questions for me ...

1. How do I debug my own scripts? I've been learning slowing but I don't know the quick way to catch an error and print to page.

2. Is there something I'm missing with this code?

if(!document.getElementById(post_id.id).innerHTML.indexOf('post_rep_box'))      document.getElementById(post_id.id).innerHTML += "<input type='text' name='post_rep_box'/>"
//I've also tried (post_id.id).value



As always, thanks in advance!!

Is This A Good Question/Topic? 0
  • +

Replies To: Adding input to div on click with if statement

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 948
  • View blog
  • Posts: 2,357
  • Joined: 15-February 11

Re: Adding input to div on click with if statement

Posted 28 November 2011 - 05:51 AM

Hey

1. The error console1 does that for you.

2. indexOf returns -1 if the text isn't found. -1 is equal to true. The only number that evaluates to false is 0. You should do this instead
if(document.getElementById(post_id.id).innerHTML.indexOf('post_rep_box') == -1)
1 Where is the Error Console?

This post has been edited by codeprada: 28 November 2011 - 05:52 AM

Was This Post Helpful? 1
  • +
  • -

#3 guyfromri  Icon User is offline

  • D.I.C Addict

Reputation: 46
  • View blog
  • Posts: 817
  • Joined: 16-September 09

Re: Adding input to div on click with if statement

Posted 28 November 2011 - 07:41 AM

Both pieces of awesome info to have. Thank you very much :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1