Solved

JQuery Changing HTML on Click

Posted on 2011-09-12
5
213 Views
Last Modified: 2012-06-21
The attached code adds a class and changes an img scource on click, but I'm sure there must be a more efficient way of doing it. Can anyone suggest a better version please?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
  <script src="http://code.jquery.com/jquery-latest.js"></script>

<style>
tr.clicked {
	background-color:#CCdddd;
}
img {
	border:1px solid red;
	width:10px;
	height:10px;
}
</style>
</head>
<body>
<table>
  <th><b>#</b></th>
    <th><b>Title</b></th>
    <th><b>Clicks</b></th>
  <tr id="1">
    <td>1</td>
    <td>xx</td>
    <td class="click"><a href="javascript:gotoURL( '1', 'pclick.asp?bid=1&tId=1','img/myimage1_on.jpg', 'img_1')"><img src="img/myimage1_on.jpg" id="img_1" /></a></td>
  </tr>
  <tr id="2">
    <td>2</td>
    <td>xx</td>
    <td class="click"><a href="javascript:gotoURL( '2', 'pclick.asp?bid=1&tId=2','img/myimage2_on.jpg', 'img_2')"><img src="img/myimage2_on.jpg" id="img_2" /></a></td>
  </tr>
  <tr id="3">
    <td>3</td>
    <td>xx</td>
    <td class="click"><a href="javascript:gotoURL( '3', 'pclick.asp?bid=1&tId=3','img/myimage3_on.jpg', 'img_3')"><img src="img/myimage3_on.jpg" id="img_3" /></a></td>
  </tr>
  <tr id="4">
    <td>4</td>
    <td>xx</td>
    <td class="click"><a href="javascript:gotoURL( '4', 'pclick.asp?bid=1&tId=4','img/myimage4_on.jpg', 'img_4')"><img src="img/myimage4_on.jpg" id="img_4" /></a></td>
  </tr>
</table>
<script>
	function gotoURL( id, url, imgUrl, imgId )
	{
		var objId = $("#"+id);
		var imgId = $("#"+imgId);
		//alert(imgUrl);
		objId.addClass("clicked");
		
		var off_img = imgUrl.replace("_on.jpg", "_off.jpg"); 
		alert(off_img);

		$(imgId).attr("src",off_img);
//window.open(url);

	}
</script>
</body>
</html>

Open in new window

0
Comment
Question by:benners70
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 36523337
maybe this
<script>
$(document).ready(function() {
  $("#myTable tr").each(function(index){
    var v = $("td:eq(0)",$(this)).html(); //alert(v);
    $("td:eq(2) a",this).click(function(){
      alert(v);
      var tr = $(this).parent().parent();
      var img = $("img",$(this));
      tr.addClass("clicked");
      alert(img.attr("src"));
      img.attr("src", img.attr("src").replace("_on.jpg", "_off.jpg"));
      alert(img.attr("src"));
    });
  });
});
</script>

<table id=myTable>
...
  <tr id="1">
    <td>1</td>
    <td>xx</td>
    <td class="click"><a><img src="img/myimage1_on.jpg" id="img_1" /></a></td>
  </tr>
...
</table>

Open in new window

0
 

Author Comment

by:benners70
ID: 36523413
There is no href anymore. I did comment this out in javascript, which I probably shouldn't have done, but I want to open a the URL after making the HTML changes.

//window.open(url);

Thanks
0
 
LVL 82

Accepted Solution

by:
hielo earned 250 total points
ID: 36523711
try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
  <script src="http://code.jquery.com/jquery-latest.js"></script>

<style>
tr.clicked {
	background-color:#CCdddd;
}
img {
	border:1px solid red;
	width:10px;
	height:10px;
}
</style>
<script>
	$(function(){
		$('.click img').live('click',function(){
			this.src=this.src.replace('on_.jpg','_off.jpg');
			$(this).closest('tr').addClass('clicked');
			window.open('pclick.asp?bid=1&tId='+this.id.replace('img_',''));
		return false;
		});
	});
</script>
</head>
<body>
<table>
  <th><b>#</b></th>
    <th><b>Title</b></th>
    <th><b>Clicks</b></th>
  <tr id="1">
    <td>1</td>
    <td>xx</td>
    <td class="click"><img src="img/myimage1_on.jpg" id="img_1" /></td>
  </tr>
  <tr id="2">
    <td>2</td>
    <td>xx</td>
    <td class="click"><img src="img/myimage2_on.jpg" id="img_2" /></td>
  </tr>
  <tr id="3">
    <td>3</td>
    <td>xx</td>
    <td class="click"><img src="img/myimage3_on.jpg" id="img_3" /></td>
  </tr>
  <tr id="4">
    <td>4</td>
    <td>xx</td>
    <td class="click"><img src="img/myimage4_on.jpg" id="img_4" /></td>
  </tr>
</table>

</body>
</html>
</body>
</html>

Open in new window

0
 

Author Comment

by:benners70
ID: 36524123
hielo: Thanks, I got what I needed from that.

<script>
      $(function(){
            $('.click img').live('click',function(){
                  this.src = this.src.replace('_on.jpg','_off.jpg');
                  $(this).closest('tr').addClass('clicked');
                  var bid = $(this).attr('id').replace('b_','');
                  var tid = $(this).attr('class').replace('t_','');
                  window.open('pclick.asp?bid='+bid+'&tId='+tid);
            return false;
            });
      });
</script>

  <tr id="1">
    <td>1</td>
    <td>xx</td>
    <td class="click"><img src="img/myimage1_on.jpg" id="b_1" class="t_11" /></td>
  </tr>
0
 
LVL 82

Expert Comment

by:hielo
ID: 36524298
In your posted html, I could see the tId is "tied" to the id of the image, but your bId was always one. Hence the reason I didn't bother initializing those variables since they seemed pointless, but seeing your 'new/augmented' markup, that would work.

Regards,
Hielo
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

829 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