0

I am trying to make a div that hides itself when clicked and shows an input.

My CSS:

#btn {
    width:100%;
    height:100%;
    display:block;
    text-align:center;
}

#i1 {
    display:none;
}

My JavaScript:

<script>
document.getElementById('btn').onclick = function() {
 document.getElementById('i1').style.display = 'block';
  document.getElementById('btn').style.display = 'none';
}
</script>

My HTML:

<div id="btn">Company</div><input id="i1" type="text" value="<?php echo"{$row['company']}";}?>">

Can anyone tell me why the Div is not hiding and, or, why the input is not showing?

Thank you for any help. All help is appreciated.

Kelsey
  • 795
  • 3
  • 15
  • 39

2 Answers2

2

The script works perfectly fine here.

You're running the script before the element is loaded. Try placing the <script> at the end of the page.

John Bupit
  • 9,458
  • 6
  • 31
  • 68
  • Spot on. As an added bit of info, OP, if you wanted to run your script in the head, you could define your function as a non anonymous function and call it, [like so](http://jsfiddle.net/8QDMs/). – user3507600 May 12 '14 at 19:46
  • 1
    @user3507600, I would avoid mixing javascript in with the dom elements as much as possible. – Dallas May 12 '14 at 19:49
  • @smerny, what do you mean by 'mixing javascript in with the dom elements' and why? – user3507600 May 12 '14 at 19:50
  • @smerny I agree. If the OP really needs the script in the head, they could place in the `onload` event. I think, [this answer](http://stackoverflow.com/a/9899701/1492578) explains the best way to do it in _pure_ JavaScript. – John Bupit May 12 '14 at 19:52
  • Thanks for the help, John. – Kelsey May 12 '14 at 19:56
  • @smerny @JohnBupit, I think I see what you guys are talking about now. I suppose you could still use a named function without mixing, also if written properly, you could make this expandable (since it looks like Kelsey plans to have multiples of these given the input is named `i1`). – user3507600 May 12 '14 at 20:00
  • @user3507600 sure, but I also prefer to avoid using indexed ID's just to appease javascript. Adding listeners based on classes and using `this` helps with that. Also much easier/cleaner to do using jquery, this is how I would do it: [JSFiddle](http://jsfiddle.net/kXAPe/1/) – Dallas May 12 '14 at 23:20
0

'Your quotes in your php are probably not helping and mess it up some.

Try:

<?php

$row['company'] = "some company";
$company = $row['company']

?>

<input id="i1" type="text" value="<?php echo $company?>">
Daniel
  • 4,365
  • 2
  • 24
  • 30