0

I am just struggling beyond belief with jQuery. I have looked at dozens of ways of adding a new input field but keep getting stuck right at the beginning. I don't see how this could be simpler.

<div class="container">
<p>If you click on me, I will disappexxxxxxar.</p>
<p id="thisone">Click this onexxxx me away!</p>
<p id="thixxsone2">Click 2pppp22222hover me too!</p>
    <input type="checkbox" id="chk">
</div>

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script> window.jQuery||document.write("<script src='../vendor/jquery/jquery-1.11.3.min.js'><\/script>")</script>
<script src="../vendor/bootstrap/js/bootstrap.js"></script>
<script src="../custom/js/custom.js"></script>

<script>
$("#chk").on("change",function(){
        $("#chk").append('<input type="text" />');
    }
)
</script>

I will be adding a if ($("#chk").is(":checked")) which seems to work fine.

I went through W3 jQuery but it was too short. Anyone know of another online "course"?

BeNice
  • 2,165
  • 2
  • 23
  • 38
  • OK sorry guys have tried solutions but not working on my simple page. I need to clean it right out and start again. Thanks for efforts will report back. – BeNice Nov 23 '15 at 00:20
  • The W3 - so far as I know - offer no jQuery 'course,' you may have referred to 'w3schools" which has no connection to the W3C *at all*, though they deliberately named themselves to imply a connection. If you want to learn about jQuery you'd be best off looking to the jQuery docs: [API.jQuery.com](http://api.jquery.com/). – David Thomas Nov 23 '15 at 00:39

3 Answers3

0

1st: what this line expect to do??

<script> window.jQuery||document.write("<script src='../vendor/jquery/jquery-1.11.3.min.js'><\/script>")</script>

2nd: good to wrap your code after document ready .. and you can use .append for your input parent() .. this will append new input after checkbox input

<script>
$(document).ready(function(){
  $("#chk").on("change",function(){
    if($(this).is(":checked")){
        $(this).parent().append('<input type="text" />');
    }     
  });
</script>
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • Mohamed thanks it was not working on my page but you had a fiddle up and that worked fine QED something wrong with my page. Have to leave for about an hour then will clean up page. Please stand by and thanks. Shukran – BeNice Nov 23 '15 at 00:23
  • Regarding your question to the OP, "*[that] line*" will simply write a script element into the document if `window.jQuery` is a false/falsey value. It's a hideous practice, but I think that's the intent of it. – David Thomas Nov 23 '15 at 01:06
  • Mohamed can't get it to do anything sorry. As to the line yep found it as a check on access to google jQuery. David why is it horrible. Absolutely non essential - just curious. – BeNice Nov 23 '15 at 01:13
  • @DavidThomas Thanks .. I just seeing he included jquery in a right way from ajax.googleapis.com cause of that I asked .. I thought that line can make conflicts with the main jquery .and I didn't use that line before in any of my projects . but I will read about this :) – Mohamed-Yousef Nov 23 '15 at 01:16
  • @OldMauiMan never mind, Just you reached the answer you need .. and that's it .. Good Luck :) – Mohamed-Yousef Nov 23 '15 at 01:17
  • Thanks. But as I want something to appear when the box is checked - that is my next step -I may be back.... You have been warned. – BeNice Nov 23 '15 at 01:25
0

With the help of an old StackOverflow question(onClick add input field), I wrote an example that uses jquery append and works:

Html

<button id="add">Add input field</button><p id="main"></p>

js

$('#add').on('click', function () {
$('#main').append('<input type="aText" id="aText"><br>');
});

https://jsfiddle.net/uhhr60pj/ Hope that helps.

Community
  • 1
  • 1
Katie
  • 16
  • 2
  • Yep thanks worked great. Now all I have do is about another 20 clever things on top of this. Cannot work out why I am so slow at jQuery. – BeNice Nov 23 '15 at 01:14
  • Don't worry, it just takes practice. You asked for a more online jQuery help, here's a list of jQuery courses and tutorials: http://www.tutset.com/frontend/jquery/ – Katie Nov 23 '15 at 15:45
0

You have one main problem with your code in your question:

$("#chk").on("change",function(){
  $("#chk").append('<input type="text" />');
});

The problem is that an <input> is a void element:

A void element is an element whose content model never allows it to have contents under any circumstances. Void elements can have attributes.

The following is a complete list of the void elements in HTML:

area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

Citation: http://www.w3.org/TR/html-markup/syntax.html#syntax-elements, accessed 01:00, 2015-11-23.

Because jQuery – almost always – fails silently it never alerts you to the fact that you're trying to achieve the impossible (literally: the impossible); so instead you have to append the elements elsewhere, such as after (or before) the given element.

// bind the anonymous function of the
// on() method to handle the change
// event of the '#chk' element:
$('#chk').on('change', function() {

  // on the assumption you only want
  // to append elements when the element
  // is in fact checked:
  if (this.checked) {

    // creating the <input> element:
    $('<input />', {

      // setting its 'type' attribute/property:
      'type': 'text',

      // setting its 'placeholder' attribute:
      'placeholder': 'input number: ' + $('input[type=text]').length

      // inserting the <input> after the '#chk' element:
    }).insertAfter(this);
  }
});
input {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <!-- I removed the irrelevant HTML -->
  <label>Click to add a new input element (when checked):
    <input type="checkbox" id="chk">
  </label>
</div>

As you can see when you check the check-box this appends the new <input> directly after that check-box. Since I don't expect that's what you want to do, I'd suggest amending the above, to access the parent – <div> – element:

// bind the anonymous function of the
// on() method to handle the change
// event of the '#chk' element:
$('#chk').on('change', function() {

  // on the assumption you only want
  // to append elements when the element
  // is in fact checked:
  if (this.checked) {

    // creating the <input> element:
    $('<input />', {

      // setting its 'type' attribute/property:
      'type': 'text',

      // setting its 'placeholder' attribute:
      'placeholder' : 'input number: ' + $('input[type=text]').length

    // appending the <input> element to the parentNode of the
    // '#chk' element:
    }).appendTo(this.parentNode);
  }
});
input {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <!-- I removed the irrelevant HTML -->
  <label>Click to add a new input element (when checked):
    <input type="checkbox" id="chk">
  </label>
</div>

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • David wow. This was just a two line code check. I will look at this later as my head is exploding. I DID however expect the input to come directly after the checkbox - this was just a tiny incremental test. (If I don't understand something I build it up in tiny ugly steps!) As to the `input` being a void element - yes I was aware of that. I did NOT realize that would be a problem (not sure why) but in any case Katie's solution worked fine. (So???) I am however extremely grateful for your full answer. Some of your code will be very helpful when I build up the next layer of ugly complexity. – BeNice Nov 23 '15 at 01:22