Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Global variable in different scripts not working

    Hi Need your help!!!

    I am new to this and seeking help. I am trying to pull data in an html using xml.
    For this i have few script used (taken from other sites) 1st script will fetch the data from xml and store in a global variable and the same variable is used in the second script.

    However when i am trying alert global variable in first script showing the value but alert in the second variable is retriving blank.

    Please help me. the html code is given below



    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet"
    href="css/my_style_sheet.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/my_style_sheet.css">
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    </head>
    <body>

    <!--<button id="addProduct">Add Product</button>-->

    <div id="someContainer" style="white-space: normal; background-color: azure;"></div>
    <div>
    <span></span>
    </div>

    <script>
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    myFunction(this);
    }
    };
    xmlhttp.open("GET", "xml/Digital.xml", true);
    xmlhttp.send();
    function myFunction(xml) {
    var i;
    var xmlDoc = xml.responseXML;
    var table="<tr></tr>";
    var x = xmlDoc.getElementsByTagName("practise");
    for (i = 0; i <x.length; i++)
    {
    var img = x[i].getElementsByTagName("imageurl")[0].childNodes[0].nodeValue;
    table += "<tr><td><br><img src='"+img+"'></td><br>" +
    x[i].getElementsByTagName("imageurl")[0].childNodes[0].nodeValue +
    "</td><td><br>" +
    x[i].getElementsByTagName("img_name")[0].childNodes[0].nodeValue +
    "</td></tr>";
    rad = x[i].getElementsByTagName("imageurl")[0].childNodes[0].nodeValue;
    alert(rad);
    }
    document.getElementById("demo").innerHTML = table;

    }
    </script>
    <script>
    window.rad = '';
    window.sree = "Sree";
    window.shre = "Shreyas";
    $( document ).ready(function() {
    var i = 0;
    /*$("#addProduct").click(function() {*/
    var output = "";
    alert(rad);

    for (i = 0; i < 10; i++) {

    output += "<div style='float:left; width:200px' class='wrapper' id='product"+i+"'>";
    output += "<textarea rows='4' cols='20' style='resize: none;' id='property"+i+"' value='"+rad+i+" "+sree+i+" "+shre+i+"'> "+rad+i+"\n"+sree+i+"\n"+shre+i+"</textarea></div>";
    if (i%5==4) {
    output +="<div style='clear:both'></div>"
    }

    };
    /*});*/
    document.getElementById("someContainer").innerHTML = output
    //.appendTo("#someContainer");
    $("input").on("click", function() {
    $("span").text("Clicked ID: " + this.value);
    });
    $("textarea").on("click", function() {
    $("span").text("Clicked ID: " + this.value);
    });
    });
    </script>

    <div id="rad"></div>
    <div id="demo"></div>
    </body>
    </html>

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    West Des Moines, Iowa
    Posts
    8,104
    Thanks
    37
    Thanked 1,086 Times in 1,082 Posts
    In the first <script> section only this line is executed:
    var xmlhttp = new XMLHttpRequest();
    Which sets up the AJAX variable. Nothing else is ever called.
    The second <script> section does execute. You end up setting in the
    Code:
    $("textarea").on("click", function() {
    	$("span").text("Clicked ID: " + this.value);
    });
    You really do not want the first script to do anything because it is wrong(Has many errors) and your HTML does not work with it.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.


 

Tags for this Thread

Posting Permissions

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