SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    To change src image for <input type="image"

    Thanks in Advance..

    I have a img tag :
    <input type="image" src="../abc.gif" name="img" onclick="clicked();"/>

    My issue is..

    in clicked js i want to change src to ../def.gif so that i can change the src dynamically ..

    I tried acessing src using id,name attributes nothing worked to me..Plz give ur valuable suggestions..

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    within your clicked function you can pass back the element that was clicked by using

    Code xhtml:
     <input type="image" src="../abc.gif" name="img" onclick="clicked(this);"/>

    then your javascript function clicked would be like

    Code javascript:
    function clicked(what) {
                    what.src = '../def.gif';
                }


  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Microsoft's IE won't be able to easily find the image because when you're using the inline event registration, IE doesn't have the this keyword reference the element, instead it just references the window itself.
    See http://www.quirksmode.org/js/this.html

    To remedy this you should use the traditional event registration instead, which has the nice effect of keeping the javascript separate from the html.

    Code html4strict:
    <input type="image" src="../abc.gif" name="img" />

    Code javascript:
    var el = document.getElementsByName('img')[0];
    el.onclick = clicked;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm confused... Every time i've used the this keyword within an inline event, it's worked. Regardless of the browser?!


  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Using the this keyword works, when you explicitly specify it.
    It's also supposed to be automatically available, but Microsoft loses it when using inline event registration.

    When specified inline and explicitly defined, the this keyword passes the element but it needs to be picked up in the function as a variable.

    Code html4strict:
    <div id="myDiv" onclick="doSomething(this)">

    Code javascript:
    doSomething(el) {
        // works with all browsers
    }

    When the inline event is called without passing the this keyword as a parameter, the function won't be able to use the this keyword to reference the element from IE.

    Code html4strict:
    <div id="myDiv" onclick="doSomething()">

    Code javascript:
    doSomething() {
        var el = this; // doesn't work with IE inline event registration
    }

    Using inline event registration has those poblems.

    Traditional event registration is where the this keyword is automatically available from all browsers

    Code html4strict:
    <div id="myDiv">

    Code javascript:
    document.getElementById('myDiv').onclick = doSomething;
    doSomething() {
        var el = this; // works on all browsers with traditional event registration
    }

    This last technique is the preferred one to use for flexibility and ease of use.
    When testing, it's also possible to explicitly pass a refrerence for the this keyword, that is to be used within the called function.

    Code javascript:
    var el = document.getElementById('myDiv');
    doSomething.call(el, text);
    function doSomething(text) {
        // the this keyword refers to el
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •