Solved

I need the correct jQuery syntax to change an image

Posted on 2011-09-30
13
193 Views
Last Modified: 2012-05-12
I have images in a jqGrid and need to reset the image to empty when resetting the row. But I am having a hard time with the syntax for getting and setting the image's path.

I have the id of the image which is unique on the page. The jqGrid is called "samples".

Please look at the code below for an idea of what I am trying to do and suggest a better syntax, since this does not work at all.

Thanks,
newbieweb
var imageField = $('#samples img[id='+imageId+']');
if ( $(imageField).src.substr(imageField.src.lastIndexOf('/')+1) != "empty.gif" ) 
{ 
     $(imageField).src = "~/Content/images/empty.gif";
}

Open in new window

0
Comment
Question by:newbieweb
  • 9
  • 4
13 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 36895563
just use

var imageField = $('#samples img[id='+imageId+']');
-->
var imageField = $('#'+imageId);
0
 

Author Comment

by:newbieweb
ID: 36895569
you mean the following?

var imageField = $('#'+imageId);

then...
if ( $(imageField).src.substr(imageField.src.lastIndexOf('/')+1) != "empty.gif" )
{
     $(imageField).src = "~/Content/images/empty.gif";
}

or...
if ( imageField.src.substr(imageField.src.lastIndexOf('/')+1) != "empty.gif" )
{
     imageField.src = "~/Content/images/empty.gif";
}
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36895570
this is also fine!

var imageField = $('#samples [id='+imageId+']');
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36895572
also this is fine

var imageField = $('#samples img[id='+imageId+']');

I dont understand where is your problem :)
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36895576
this should do

  alert(imageField.attr("src"));
  if ( imageField.attr("src").substr(imageField.attr("src").lastIndexOf('/')+1) != "empty.gif" ) {
     $(imageField).src = "~/Content/images/empty.gif";
  }
0
 

Author Comment

by:newbieweb
ID: 36895577
Oh, my syntax was okay?

Maybe it's in my use of the following?

 $(imageField).src

I also tried:

imageField.src

with no success
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:newbieweb
ID: 36895579
Ok, I will try that and let you know in a minute...
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36895580
your issue is not to find the image, but to get the src attribute :) which I posted above...
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36895581
or no jQuery, simple javascript will do the same


  alert(document.getElementById(imageId).src);
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 36895584
also you should use

$(imageField).src = "~/Content/images/empty.gif";
-->
imageField.attr("src", "~/Content/images/empty.gif");
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 36895590
so, final syntax

var imageField = $('#samples img[id='+imageId+']');
if ( imageField.attr("src").substr(imageField.attr("src").lastIndexOf('/')+1) != "empty.gif" ) {
 imageField.attr("src","~/Content/images/empty.gif");
}
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 500 total points
ID: 36895592
one more issue... ~ does not work on clientside (unless you really have a folder with name ~)

you should use

 imageField.attr("src","/Content/images/empty.gif");
0
 

Author Closing Comment

by:newbieweb
ID: 36895597
Thanks! Unfortunately the web service is down and I can not test this code. But it looks great. Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now