Solved

onclick event requires double click to load

Posted on 2009-06-28
3
406 Views
Last Modified: 2012-05-07
I have an onclick event handler which runs a script to test if and element id has innerHTML == nothing.  Problem is when I click on a table row it requires a double click to load the innerHTML of the call.  Hope this makes sense as I am knew to javscript.
<script>
function loadHub(id, jid){
	var testLoad = document.getElementById('loadDiv' + id);
	if(testLoad.innerHTML == '') {
		loadJob(id, jid);
	} else {
		unloadJob(id);
	}
}
	
	
function loadJob(id, jid){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('loadDiv' + id);
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
 
		}
	}
	var queryString = "?id=" + id + "&jid=" + jid;
	ajaxRequest.open("GET", "loadjob.php" + queryString, true);
	ajaxRequest.send(null); 
}
 
function unloadJob(id){
	var ajaxClose = document.getElementById('loadDiv' + id);
	ajaxClose.innerHTML = "";
 
}
</script>
 
<?php
while($rows=mysql_fetch_array($result)){
	$jid = $rows['JID'];
    $cid = $rows['CID'];
$total = mysql_num_rows($result);
if(is_odd($i)){
echo "<tr class = 'odd' onclick='loadHub($i, $jid)'>";
} else {
echo "<tr onclick='loadHub($i, $jid)'>";
}
echo"<td>".$rows['JID']."</td>
     <td>".$rows['CID']."</td>
     <td class = 'bunked'><ul><li>".$rows['pname']."</li>
	 						  <li class = 'bunked'>".$rows['zname']."</li></ul></td>
	 <td class = 'bunked'><ul><li>".$rows['name']."</li>
	 						  <li class = 'bunked'>".$rows['address']."</li></ul></td>
	 <td class = 'bunked'><ul><li>".formatPhone($rows['home'])."</li>
	 						  <li class = 'bunked'>".formatPhone($rows['cell'])."</li></ul></td>
     <td>".$rows['summary']."</td>
  	 </tr>";
	 if(is_odd($i)){
			echo "<tr class = 'odd' onclick='loadJob($i, $jid)'>";
		} else {
			echo "<tr onclick='loadJob($i, $jid)'>";
		}
	 if(strlen($rows['description']) < 200) {
	 echo "<td colspan = '6'>".nl2br($rows['description'])."</td>";
	 } else {
	 echo "<td colspan = '6'>See Full Description</tr>"; 
	 }
	 echo"</tr>
	 <tr class='space'><td id = 'space".$i."' colspan = '11'><div id = 'loadDiv".$i."'> </div></td></tr>";
	 
	  
$i++;
}
?>

Open in new window

0
Comment
Question by:cone13cone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Assisted Solution

by:Chad Haney
Chad Haney earned 100 total points
ID: 24733604
it is taking a double click to work because the first click it is not = '', it is null or nothing.  So your code is setting the value to '' on the first click then the second click it will respond.  try flipping the check as follows

function loadHub(id, jid){
        var testLoad = document.getElementById('loadDiv' + id);
        if(testLoad.innerHTML != '') {
               unloadJob(id);
        } else {
               loadJob(id, jid);
        }
}

Open in new window

0
 

Author Comment

by:cone13cone
ID: 24751164
Dachusa the above did not work, its still require a dbl click the first time to load. =\
0
 

Accepted Solution

by:
cone13cone earned 0 total points
ID: 24751170
This fixed it, thanks.
function loadHub(id, jid, cid){
        var testLoad = document.getElementById('loadDiv' + id);
        if(!testLoad.innerHTML) {
               unloadJob(id);
        } else {
               loadJob(id, jid, cid);
        }
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Worldmap 1 28
Anyone familiar with PhotoSwipe? 3 19
how to customize the text in the legend in highcharts 3 35
Compute age Html 2 27
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question