how to add image based on if condition in JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 123123om
    New Member
    • Apr 2012
    • 14

    how to add image based on if condition in JavaScript

    Hi All,
    i am asking this question for 2nd time, but still didn't get the reply. anyone pls help me how to insert image based on if condition in javascript. this is my code:
    Code:
    if(desc == "Cloudy")
    {
    alert("cloudy");        $("input#desc").val(response.getElementsByTagName("desc").item(0).firstChild?response.getElementsByTagName("desc").item(0).firstChild.data : "");
    document.write(unescape("<img src='images/sunny.gif'>" +desc.innerHTML));
    }
    but document.write( ) opens a new window how to avoid it ? my application should display a weather forecast with respected image

    this is my client code:
    Code:
    <li>
    <label for="desc"><span class="required">Description</span></label>
    <input id="desc" name="desc"  type="text" class="text required"  disabled="disabled"/>
    </li>

    please somebody help me
    Last edited by Dormilich; Apr 25 '12, 12:35 PM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    but document.write( ) opens a new window how to avoid it ?
    document.write( ) doesn’t open a new window (that’s window.open()). it recreates the page if used after page load.
    Last edited by Dormilich; Apr 25 '12, 12:40 PM.

    Comment

    • 123123om
      New Member
      • Apr 2012
      • 14

      #3
      thanks for the reply. but no luck, when i tried with document.write( ) it vanishes the details of current window and displays the image in new window. please let me know is there any other way to do this ?
      Code:
       document.URL = "../images/sunny.gif";
                                                       document.images.src = "../images/t-showers.gif";
      i have tried with above code also but still no luck.. please help me.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        but no luck, when i tried with document.write( ) it vanishes the details of current window and displays the image in new window.
        that’s what I said with "recreating the page".


        i have tried with above code also but still no luck
        of course, document.URL is read-only (and wouldn’t be an image either) and document.images is a List, not an image element.

        what you would need to do is insert an <img> element into the document tree (but since I don’t have enough information about the where and how, I can’t provide even demo code).

        besides that, Strings do not have an .innerHTML property. or vice versa, HTMLElements do not equal Strings (unless you get the element stringification right)

        Comment

        • 123123om
          New Member
          • Apr 2012
          • 14

          #5
          thanks for the reply. u said as that’s what I said with "recreating the page". could you let me know how to do this ?
          what you would need to do is insert an
          Code:
          <img>
          element into the document tree: please let me know how to add ?

          and also could anyone post the demo code ? which will help me a lot. thanks in advance

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            using a simplified example where I insert an image into a document
            Code:
            <!DOCTYPE html>
            <html>
            	<head>
            		<title>Test Centre</title>
            		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            	</head>
            	<body>
            		<div id="test"></div>
            		<script type="text/javascript" src="test.js"></script>
            	</body>
            </html>
            Code:
            // test.js
            document.getElementById("test").innerHTML = '<img src="pic.png" width="200" height="100" alt="a demo picture">';
            // or
            // document.querySelector("#test").innerHTML = '<img src="pic.png" width="200" height="100" alt="a demo picture">';
            Last edited by Dormilich; Apr 26 '12, 09:46 AM.

            Comment

            • 123123om
              New Member
              • Apr 2012
              • 14

              #7
              Last edited by Dormilich; Apr 26 '12, 10:54 AM.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I get a connection error with that.

                Comment

                • 123123om
                  New Member
                  • Apr 2012
                  • 14

                  #9
                  hi dormilich,


                  this is how m getting dynamic weather data as expected but in description field i need to add image based on weather condition as if its cloudy or sunny etc
                  here is my code after modification
                  Code:
                  var desc = response.getElementsByTagName("desc").item(0).firstChild?response.getElementsByTagName("desc").item(0).firstChild.data : "";
                  if(desc == "Cloudy")
                  {
                  alert("cloudy");
                  document.querySelector("#desc").innerHTML = '<img src="images/cloudy.gif" alt="">';          $("input#desc").val(response.getElementsByTagName("desc").item(0).firstChild?response.getElementsByTagName("desc").item(0).firstChild.data : ""); 
                  }


                  Code:
                  					
                  <li>
                  <div id="desc">
                  <label for="desc"><span class="required">Description</span></label>
                  <input id="desc" name="desc"  type="text" class="text required"  disabled="disabled"/>
                  </div>
                  </li>
                  i am new to javascript,i would appreciate your more help, pls help me.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    does it work?

                    Comment

                    • 123123om
                      New Member
                      • Apr 2012
                      • 14

                      #11
                      that is the link for my web page which is after entering the ZIP code which shows details of weather forecast

                      Comment

                      • 123123om
                        New Member
                        • Apr 2012
                        • 14

                        #12
                        does it work?
                        no still not working where to use div tag to define id as i am using label tag to define and input tag to get datas dynamically

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          a little bit more information besides "still not working" would be incredibly helpful.

                          Comment

                          • 123123om
                            New Member
                            • Apr 2012
                            • 14

                            #14
                            by using
                            Code:
                            document.getElementById("test").innerHTML = '<img src="pic.png" width="200" height="100" alt="a demo picture">';
                            its not navigating the page and not displaying the image as well and other details are displaying normally

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              above code would require you to actually have an element with the ID of "test" …

                              and why should it navigate the page (whatever that is supposed to mean*) at all?



                              * - you can navigate a ship or a vehicle (if you get what I mean)

                              Comment

                              Working...