0

I am creating an image in jQuery like this but the border attribute doesn't seem to work... Is my syntax wrong?

    var img =
         $('<img>', {
            'title': 'Event  (Event '+wavy_counter+')',
            'class': 'listed_event',
            'id': 'wavy_event_'+wavy_counter+'',
            'data-count': ''+wavy_counter+'',
            'src': 'final_tutorial_buttons/wavy.png', 
            'height': '50', 
            'width': '50',
            'border': 'solid',
            'border': '2px',
            'border': 'red',
            'onclick': 'highlight.call(this);', 
            'data-start': '', 
            'data-end': ''
        });

I get a border but is black instead of red.

4 Answers 4

4

I think it should be:

'style': 'border: 2px solid red',

It's been a long time I don't use the "native" HTML border attribute, but I think it only sets the border thickness. To define all the properties you want, you need CSS, hence the style attribute.

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

1 Comment

This is the only correct answer. If anyone wonders who down voted all the other answers, it was me, and I did it because all the other answers are wrong. FYI, you can use css:{border:'2px solid red'} and it will use jQuery's .css() method. This way you can use an object to set multiple styles.
2

Try combining the definitions and using the attr method instead:

$('<img />').attr({
    /* ... definitions ... */,
    src : 'final_tutorial_buttons/wavy.png',
    style : 'border:2px solid red;',
    /* ... definitions ... */,
});

Comments

0
$(document).ready(function() {

   var img = $("<img />").attr("src","http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif").css({border: "2px solid red"});


   $("body").append(img );

});

​ ​ Demo: http://jsfiddle.net/CXn6v/

Comments

-2

Just a suggestion: have you tried using the Hex color code for red? #FF0000

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.