Tell me how you can update a specific 'div' when you click on a button.

<script type="text/javascript"> function searchClicked() { var urequest = document.getElementById("searchform").value var scriptid = document.getElementById('myclass2') scriptid.src = 'http://gdata.youtube.com/feeds/videos?vq=' + urequest + '&max-results=8&alt=json-in-script&callback=showMyVideos&orderby=relevance&sortorder=descending&format=5&fmt=18' } </script> <div id="searchblock" class="searchblock"> <div class="formqwerty"> <input id="searchform" class="inputbox" type="text" maxlength="40" size="40" onfocus="if (this.value=='Search for something here') this.value='';" name="q"> <input onclick="searchClicked()" id="search-submit-button" class="button" type="submit" value="" onfocus="this.blur();" name="s"> </div> <div id="scriptdiv"> <script id="myclass2" src="http://gdata.youtube.com/feeds/videos?vq=&max-results=8&alt=json-in-script&callback=showMyVideos&orderby=relevance&sortorder=descending&format=5&fmt=18"></script> </div> </div> 

You need to update the div with id = "scriptdiv", which contains the script with id = "myclass2". Now when you enter text into the search bar and click on the button, the "src" parameter of script id = "myclass2" is simply updated. But there is no request for an updated script. How can I update the contents of the div block id = "scriptdiv"?

    1 answer 1

    Slightly changed the behavior of the function, now you do not need to contact myclass2

     function searchClicked() { var urequest = document.getElementById("searchform").value; var scriptDiv = document.getElementById('scriptdiv'); var script= document.createElement('script'); script.type = 'text/javascript'; script.src = 'http://gdata.youtube.com/feeds/videos?vq=' + urequest + '&max-results=8&alt=json-in-script&callback=showMyVideos&orderby=relevance&sortorder=descending&format=5&fmt=18'; scriptDiv.innerHTML = ''; scriptDiv.appendChild(script); } 

    Well, on JS Fiddle you can check.

    • Thanks Roman! - Saturn
    • one
      Not at all, good luck! But in general, look towards AJAX for the future. - Zhukov Roman