How do i set image size in javascript file?

Discussion in 'JavaScript' started by black.hat, Mar 26, 2010.

  1. #1
    I have small javascript file that show two images, i want to set the size of those two images,

    for e.g. in html i will need to use this:

    width="xx" height="xx"

    what about javascript?

    here is my code:

    $().checkbox({checked: "/img/checked.jpg", unchecked: "/img/unchecked.jpg"})
    Code (markup):
     
    black.hat, Mar 26, 2010 IP
  2. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    As you are using jquery thats very simple. You can either set the width and height attribute or assign some css to the image. Have a look at the jquery methods attr and css
     
    Nyu, Mar 26, 2010 IP
  3. black.hat

    black.hat Active Member

    Messages:
    157
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Sorry but i am really not familiar with JS, can you add it to my code your self? thanks
     
    black.hat, Mar 26, 2010 IP
  4. hugsbunny

    hugsbunny Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can get the element from the page first by id like :

    $('XXX').width= ... , height = ...

    using jQuery ( which u r ) , is very simpler instead of getElementByID or other ways like document.images
     
    hugsbunny, Mar 27, 2010 IP
  5. inegoita

    inegoita Member

    Messages:
    54
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #5
    hi

    i think that using jquery you have to do like this

    $('image element selector').width(100).height(200); ///you can do this as width and height are functions and they return the jquery object

    if you want to use the jquery css function you have to do this

    $('image element selector').css({width:"100px",height:"200px"});

    hope it helps
    John
     
    inegoita, Mar 28, 2010 IP
  6. black.hat

    black.hat Active Member

    Messages:
    157
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #6
    man what i need to enter in ('XXX')??

    also $('image element selector').??
     
    black.hat, Apr 2, 2010 IP
  7. inegoita

    inegoita Member

    Messages:
    54
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    48
    #7
    if you are not familiar with jQuery selectors I suggest reading the online documentation on the jQuery site

    as a crash course in jQuery selectors:
    - selectors are ways of identifying DOM elements
    - jQuery selectors are similar to CSS selectors (probably not very helpful info for you)
    simple selectors:
    .cssClassName - selects elements that have a specific class
    or
    #id - selects the element with the specified id
    or
    tagname - selects all element having the specific tagname

    for example if you have something like

    ....
    <div class='imageHolder'>
    <img src='something.jpg' id='myImage'/>
    </div>
    ...


    $('img') - will select (tagname selector) all the images in the document so every transformation you apply will reflect on all images; in our case if there arent any images in the document it will select the on image
    $('.imageHolder') - will select all the elements having the class name imageHolder; in our case the div
    $('#myImage') - will select the image with the id myImage; in our case the one image

    you can also combine selectors
    $('img#myImage') - select all the img elements having the id myImage

    $('div.imageHolder') - select all the div elements that have the classname imageHolder

    also there are more advanced selectors like
    $('div.imageHolder > img#myImage') - select the div with the classname imageHolder and then select the direct child img tag with the id myImage
     
    inegoita, Apr 6, 2010 IP