0

I have a simple input line and want to append whatever has been entered each time somebody pushes the OK button. Sounds simple so far, still I am unable to get it working HTML:

<p>
<input name="todo" id="todo" type="text" value="Set Me To Value" size="32" maxlength="30" /> 
<p id="status">Ok</p>
<br>

JQuery:

$(document).ready(function(){
$('#status').on('click', function(){
var input = $('input[name=todo]').val();
$('<br><b id="taskz">'+input+'</b> - <b id="statusz">Ok</b>').after('#status');
 });
 });

I also tried my luck with append or appendTo, but both times unsuccessfully. Just in case here is the JSFiddle: http://jsfiddle.net/NRWzE/

Paul
  • 51
  • 1
  • 1
  • 10

5 Answers5

2

Try use jquery insertAfter:

$(document).ready(function () {
    $('#status').on('click', function () {
        var input = $('input[name=todo]').val();
        $('<br><b id="taskz">' + input + '</b> - <b id="statusz">Ok</b>').insertAfter('#status');
    });
});
Mohamed AbdElRazek
  • 1,654
  • 14
  • 17
2

It looks like you meant to use:

$('#status').after('<br><b id="taskz">'+input+'</b> - <b id="statusz">Ok</b>');

(see after docs)

or, alternatively insertAfter:

$('<br><b id="taskz">'+input+'</b> - <b id="statusz">Ok</b>').insertAfter('#status');
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • Why would you put it after, instead of inside? Starting with a `
    ` implies it is part of a `

    ` but there's no explicit `

    ` when you're done.

    – ErikE May 30 '13 at 17:49
  • @ErikE, because OP seemed to imply that the content should appear **after** the "button". – zzzzBov May 30 '13 at 18:01
2

.after() works, but you need to set it up correctly, according to documentation it should be:

.after( content [, content ] )

So the right way is:

$("#status").after('<br><b id="taskz">'+input+'</b> - <b id="statusz">Ok</b>');
hyunkeln
  • 429
  • 2
  • 9
1

Try this:

$('#status').click(function(){
    var input = $('input[name=todo]').val();
    $('#status').append('<br><b id="taskz">'+input+'</b> - <b id="statusz">Ok</b>');
});
pietroalbini
  • 336
  • 3
  • 13
1

There are a few things going on, but the big thing is that you need to research more how after, append and appendTo work. Here's the basic syntax difference in the methods that share a name but one has To on the end:

  • Newcontent.appendTo(existingElement) returns newElements.
  • existingElement.append(newContent) returns existingElement.

Additionally, after puts the new element as a sibling of the reference element, whereas append puts the new element as a child. This is an important difference.

So, try this script then:

var taskid = 1;

$('#valueform').on('submit', function(){
    var input = $('#todo').val();
    $('<br><span id="task' + taskid.toString() + '">' + input
       + '</span> - <span id="status' + taskid.toString()
       + '">Ok</span>').appendTo('#status');
    taskid += 1;
    $('#todo').focus().select();
    return false;
});
$('#todo').focus().select();

See a Live Demo at JSFiddle

Here's the supporting HTML:

<form id="valueform">
<input name="todo" id="todo" type="text" value="Set Me To Value" size="32" maxlength="30" />
<input type="submit" value="OK" id="okbutton">
</form>
<p id="status"></p>

There are some other concerns:

  • I recommend you study which HTML elements are allowed within which HTML elements.
  • Instead of putting a <b> tag on each item, use CSS. Additionally, if there is semantic importance for the bolding, then use <strong> instead. <b> also should probably not take an id because it is a presentation tag, not a content tag. When thinking of presentation vs. semantics, one must consider screen readers or browsers that cannot render bold text--in that case, <strong> will allow them to emphasize the text in another way if needed.
  • Get familiar with the jQuery documentation. Careful reading of what exactly each function does, the object it works on, the parameters expected, and the values returned will enable you to get past barriers in the future without having to ask here.
  • It looked to me like you wanted to put the new content inside of the #status paragraph, not after it. So I wrote my script that way. If you put it after the way you wrote it, then the most recent status will be on top--but then you have non block-level content (starting with your <br>) outside of any block-level element. So you should be appending <p> elements, or you should put your content inside the existing <p>.

Note: I added a form and made the button type submit instead of button to get easy Enter-key handling. It doesn't have to be this way.

ErikE
  • 48,881
  • 23
  • 151
  • 196
  • 1
    "`` is a deprecated tag" - [what gave you that idea? It's not deprecated in any way.](http://www.w3.org/html/wg/drafts/html/master/single-page.html#the-b-element) – zzzzBov May 30 '13 at 18:03
  • 1
    My apologies. I meant that it's deprecated by the web development community, not necessarily by the W3C, though I may have overstated the case. I have changed the wording and cleaned up my recommendation a bit. Please see [this](http://html5doctor.com/i-b-em-strong-element/), [this](http://stackoverflow.com/a/271776/57611) and [this](http://stackoverflow.com/questions/5780166/is-is-bad-practise-to-use-the-b-tag). – ErikE May 30 '13 at 18:39
  • 1
    The W3C *is* the web development community. `` tags are just fine, along with `` tags so long as you're using them as described in the HTML specification, which this use case actually meets, there's no semantic emphasis on the contents, it's purely for a utilitarian purpose in this instance. – zzzzBov May 30 '13 at 19:29
  • 1
    I'm sorry, we'll just have to disagree. What is embodied in official standards is not the same as what the leading edge of the professional web development community is doing. That the `` tag is *valid* doesn't mean it is *best*. I will repeat myself and **categorically say that using CSS is better than using `` tags**. If you disagree, that's fine, but please don't pretend that you have some kind of industry support for that position. – ErikE May 30 '13 at 20:50
  • From the spec which I linked to above: "The b element represents a span of text to which attention is being drawn for utilitarian purposes without conveying any extra importance and with no implication of an alternate voice or mood" If you disagree with how the specification is written, I highly recommend that you participate in the w3c mailing list for HTML and voice your objections. If you want the specification to reflect what you believe the professional web development community is actually doing, you'll need to contribute. – zzzzBov May 30 '13 at 21:47
  • [I **never** said that `` tags were *better* than using CSS, I said "`` tags are just fine" They are appropriate markup in this case.](https://yourlogicalfallacyis.com/strawman) – zzzzBov May 30 '13 at 21:49
  • And I **never** said that `` tags aren't fine (in the sense of "allowed by the W3C"), just that they aren't best. And so professionals *deprecate* them. They are **not** "appropriate" markup in this case because there is a better way--CSS. And P.S.: I am part of the professional web development community, and I am not part of the W3C, so your statement that "The W3C *is* the web development community" is proven wrong (all it takes is one counterfactual, right?). – ErikE May 30 '13 at 22:06
  • And P.P.S. Why are you so hostile? Seriously. – ErikE May 30 '13 at 22:15