0

Javascript:

        var header = $("#inputHeader").val();

    function coolInput() {
        if (header === "") {
            $("#displayHeader").text("Type Something!");
        }
        else if(header !== "") {
            $("#inputHeader").keyup(function () {
                var value = $(this).val();
                $("#displayHeader").text(value);
            }).keyup();
        }
    }

    setInterval(coolInput, 1);

HTML:

        <blockquote>
            <p id="displayHeader">jQuery no worky.</p>
            <small><?php echo $_SESSION['user']; ?></small>
        </blockquote>

My jQuery does work because the paragraph gets changed to "Type Something!", but when I enter something in the textbox the value does not change.

3 Answers 3

3

The return value of val() is a plain string. It isn't live updated.

You need to call val() on the jQuery wrapper object each time you call the coolInput function.

var inputHeader = $("#inputHeader");
function coolInput() {
    var header = inputHeader.val();
Sign up to request clarification or add additional context in comments.

1 Comment

@user1185220 — Not to get the value for header that you are testing against.
1

Why you are applying the keyup event handler every 1 ms. Just define the keyup event once.

Sample

$('#foo').on('keyup',function() {
    $bar.text($foo.val());
});

Inside the keyup handler function, you can easily test for the value.
If this is of no avail for you, let me know and i'll remove the post from your thread.

Edit: Just noticed the 2nd .keyup() right behind the one with the handler function. Why did you put it there?

Comments

0

You don't need to specify keyup event every one millisecond. That's what your script do.

You can change this with :

var header = $("#inputHeader");

function coolInput() {
    if (header.val() === "") {
        $("#displayHeader").text("Type Something!");
    }
    else if(header.val() !== "") {
        $("#displayHeader").text(header.val());
    }
}

setInterval(coolInput, 1);

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.