1

I'm trying to make a code live preview form with jQuery, so when people type in the input, the related <span> will update.

JQuery:

var $comment = '';
$(function() {
    $("input").keyup(function() {
        var currClass = $(this).attr('class');
        $comment = $(this).val();
        $('span.'+currClass).html($comment);
        return false;
    });
});

HTML:

<label for="image-left-url">Image URL: </label>
<input id="image-left-url" class="image-left-url" value="http://"></input>

<code><pre>&lt;img src=&quot;<span class="image-left-url"></span>&quot;/&gt;</pre></code>

My question is, for some inputs I have the value="xx" part. How can I make it so that if the input is blank, value="xx" doesn't show up (so that it doesn't show up in the <span>)?

3 Answers 3

1
var $comment = '';
$(function() {
    $("input").keyup(function() {
        var currClass = $(this).attr('class');
        var _val = $(this).val();
        $comment = (_val != 'xx') ? _val : '';
        $('span.'+currClass).html($comment);
        return false;
    });
});

http://jsfiddle.net/UX4a9/

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

Comments

0

Something like this

var $comment = '';
$(function() {
    $("input").keyup(function() {
        var currClass = $(this).attr('class');
        $comment = $(this).val();
        if(!$comment){
           $(this).val('xx');
           $('span.'+currClass).hide();
        } 
        else{
           $('span.'+currClass).html($comment).show();
        }

        return false;
    });
});

2 Comments

This hides the span if there is ever a comment.
Thanks @AlienWebguy, but I didnt understood the question well.
0
var $comment = '';
$(function() {
    $("input").keyup(function() {
        var currClass = $(this).attr('class');
        $comment = $(this).val();
        var notblank = ($comment != $(this).attr("value"));
        $('span.'+currClass).html(notblank ? $comment : '');
        return false;
    });
});

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.