2

Consider:

  $( "#scanInDialogItems tr td:nth-child( 3 )").mouseenter( function() {
     var q = $( this ).html();
     $( this ).html( "<input type='number' style='text-align:right width:50px' min='1' value='" + q + "'/>" );
  }).mouseleave( function() { 
     var q = $( this ).find( "input" ).val();
     $( this ).html( q );
  });

Whenever the user hovers over a cell in the third column of my table, the content is replaced with an input element. Works like a charm.

The problem is that the inline styling for this element is not properly applied. E.g. the text in the box for example aligns left - the default. Also the width is not set.

I read in an earlier question that dynamically created input elements need to be refreshed when created:

Styles not getting applied properly in dynamically created radio buttons

So I tried adding this:

     $( this ).find( "input" ).textinput( 'refresh' );

And or this:

     $( this ).find( "input" ).textinput();

But neither of those works. My target platform is the latest chrome.

Am I not refreshing input correctly? Or is there some other problem?

3
  • I think that link only applies to jQuery mobile. Anyways, can you give the input a class and then define the styling with CSS for that class? Does the min attribute work if you try to type in it? Like if you try to type in -23, does it show an error on the page? Commented Nov 5, 2012 at 17:01
  • Comment rather than answer, but it may help... Is it necessary to add your styles inline with your new markup, could they be anticipated in prior CSS instead? Commented Nov 5, 2012 at 17:03
  • I agree that it is better to put styles in css instead of inline. But inline is easier during development. I do a pass at the end creating styles in .css once I everything works. Commented Nov 5, 2012 at 17:05

1 Answer 1

3

You've missed a semicolon between text-align and width in the inline style...

$( "#scanInDialogItems tr td:nth-child( 3 )").mouseenter( function() {
    var q = $( this ).html();
    $( this ).html( "<input type='number' style='text-align:right;width:50px' min='1' value='" + q + "'/>" );
}).mouseleave( function() { 
    var q = $( this ).find( "input" ).val();
    $( this ).html( q );
});

I just tried a before & after in jsfiddle and that's all that was wrong with it :)

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.