1

I want to create a html link with some styling in it.

right now I'm using:

$('<a/>').text('sample')
         .attr('href', 'http://www.google.com/').appendTo('body');

$('a').css({
               float: 'right',
               fontFamily: 'Verdana, Arial, Helvetica, sans-serif',
               position: 'static',
               display: 'inline',
               visibility: 'inherit'
          });

but instead create and refer the tag 2 times, is there are simpler way to do it? like:

$('<a/>').text('sample')
         .attr('href', 'http://www.google.com/').appendTo('body');
         .css({
               float: 'right',
               fontFamily: 'Verdana, Arial, Helvetica, sans-serif',
               position: 'static',
               display: 'inline',
               visibility: 'inherit'
          });

3 Answers 3

2

You have semicolon at wrong place that terminates method chaining.

 .attr('href', 'http://www.google.com/').appendTo('body');
                                                        ^^^  

You code would be

$('<a/>').text('sample')
     .attr('href', 'http://www.google.com/').appendTo('body')
     .css({
           float: 'right',
           fontFamily: 'Verdana, Arial, Helvetica, sans-serif',
           position: 'static',
           display: 'inline',
           visibility: 'inherit'
      });
Sign up to request clarification or add additional context in comments.

Comments

0

Try with

$('<a/>').text('sample')
     .attr('href', 'http://www.google.com/').appendTo('body')
     .css({
           float: 'right',
           fontFamily: 'Verdana, Arial, Helvetica, sans-serif',
           position: 'static',
           display: 'inline',
           visibility: 'inherit'
      });

you are terminating method chaining after giving "appendTo" so "css" will not be applicable there....try to remove that semicolon ';' at the end of appendTo('body') to include "css" also

Comments

0
$('<a>').text('sample')
         .attr('href', 'http://www.google.com/')
         .css({
               float: 'right',
               fontFamily: 'Verdana, Arial, Helvetica, sans-serif',
               position: 'static',
               display: 'inline',
               visibility: 'inherit'
          }).appendTo('body');

Demo: http://jsfiddle.net/wQLmw/

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.