0

how do you check if a string/text still exist in an HTML element particularly <input>?

i have this code $("input").bind('paste', function(e) { that captures paste events and checks if the pasted text contains a youtube url, parse it via regex and simply print the youtube url in a <div> element.

how can i make javascript to delete the printed data, if the link is removed/deleted in the input element

here is a sample: http://jsfiddle.net/8Pvr4/5/

input any youtube url in the textfield, and it should display something. then remove the url, the data still remains.

basically i just want the printed output to disappear once the existence of the url is removed in my input

initial thought was adding some onChange event to the input but im not quite sure how can i do it without messing the current code.

2
  • Have you tried binding a .keyup? Commented Oct 28, 2013 at 3:49
  • yes. i did already. but the problem with that. it recognizes the ctrl and v so it prints the result twice. and pasting using mouse is not recognized. Commented Oct 28, 2013 at 3:55

1 Answer 1

1

Try this with keyup event like

$("#input").on({
    'paste': function (e) {
        $("#print").html('');
        var val = e.originalEvent.clipboardData.getData('text/plain');
        if (youtube.test(val)) {
            var yurl = val.match(youtube)[0];
            $("#print").html(yurl);
        }
    },
        'keyup': function (e) {
        $("#print").html('');
        var val = this.value;
        if (youtube.test(val)) {
            var yurl = val.match(youtube)[0];
            $("#print").html(yurl);
        }
    }
});

Demo

Sign up to request clarification or add additional context in comments.

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.