Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery Changing HTML on Click

Posted on 2011-09-12
5
Medium Priority
?
240 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
[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
  • 2
5 Comments
 
LVL 59

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 1000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

688 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