find a link within the document with JavaScript

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2033 Views - Last Post: 07 January 2010 - 09:20 PM

#1 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

find a link within the document with JavaScript

Posted 05 January 2010 - 12:25 PM

I want to translate this into text:
'document.star_1' + '.src="' + pic_selected[l] + '"';


I thought something like this would work, but I guess I can't do document. ?
'document.star_1' + '.innerHTML="' + txt_unselected[ny] + '"';

Is This A Good Question/Topic? 0
  • +

Replies To: find a link within the document with JavaScript

#2 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: find a link within the document with JavaScript

Posted 05 January 2010 - 01:09 PM

You are very vague in what you want to do.

The first code you have looks like it is just changing an image, and the second one is just changing the text.

What are you looking to do?

Please try to explain a little bit rather than just posting code, and I may be able to get something goin for you.

This post has been edited by Moshambi: 05 January 2010 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: find a link within the document with JavaScript

Posted 05 January 2010 - 02:06 PM

Well, the first one is code that someone else wrote, and I want to edit it so that it works for text (I'm allowed to do that). The first one has linked images that change back and forth, and I want just plain links that change back and forth.
But, I guess it's different because the link is between the html tags instead of inside a tag?
I thought maybe I could change the link if I put a href="" name="star_1", but it says it can't find star_1. That, and that .innerHTML thing I found somewhere online, it said it doesn't work for all browsers, so maybe there's some other way of doing it...
Maybe I could make a variable to hold the link text and change that somehow? But, I don't know how to change the text instead of just printing more...

This post has been edited by asj127: 05 January 2010 - 02:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: find a link within the document with JavaScript

Posted 05 January 2010 - 08:28 PM

Oh ok now I see. You just want to change the link text after some event...

Give the link an ID attribute and this will work much better:

<a href="#" id="link1" onclick="changeLink();">Link 1</a>



Then you could access it in Javascript like this:


function changeLink()
{
	 document.getElementById("link1").innerHTML = "Link 1 changed!";
}





Hope that helps!
Was This Post Helpful? 0
  • +
  • -

#5 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: find a link within the document with JavaScript

Posted 06 January 2010 - 08:47 AM

I saw that somewhere but it didn't work when I tried it before. Now it almost works but not quite...
There's five links, one after the other, and I want each of them to change. The link I'm touching should turn to the over text, the ones before it should be selected, and the ones after it should be unselected. Right now, The very first link does what it's supposed to, but every link after that says undefined.
for (n = 1; n <= 5; n++)
{
var nx = n-1;
if (n < star_num)
{
document.getElementById("star3_"+n).innerHTML = txt_selected[nx];
} else if (n == star_num)
{
document.getElementById("star3_"+n).innerHTML = txt_over[nx];
} else { 
document.getElementById("star3_"+n).innerHTML = txt_unselected[nx];
}


This is from the original link, in a similar loop:
Id="star3_' + n + '"


The original code, that uses images, puts it in a variable and then passes it as a parameter into something else... It does the same thing when I do it that way.
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_selected[nx]+'"';

This post has been edited by asj127: 06 January 2010 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: find a link within the document with JavaScript

Posted 06 January 2010 - 10:19 AM

do you have all the arrays setup? (txt_selected[], txt_over[], txt_unselected[])

Other than that I don't really see why it wouldn't be working.

You have all your links ID's set to star3_0, star3_1, star3_2, star3_3 and so on right?
Was This Post Helpful? 0
  • +
  • -

#7 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: find a link within the document with JavaScript

Posted 06 January 2010 - 03:54 PM

Maybe there's something that conflicts with it somewhere or something... I tested the part that gives the id's in a way, and that seems to work...
Okay, this is the original code, which is for the Smackjeeves site. I'm trying to show the users of the site the different possibilities (meaning, I suddenly think of something hard and am compelled to prove it's possible, even though I don't have to).
<script language="Javascript" type="text/javascript">
/* <![CDATA[ */
var dir = "http://www.smackjeeves.com/templates/default/images/";
var pic_unselected = "star-unselected.gif";
var pic_selected = "star-selected.gif";

<!-- BEGIN switch_user_has_voted -->
var default_rating = {switch_user_has_voted.RATING};
<!-- END switch_user_has_voted -->
<!-- BEGIN switch_user_hasnt_voted -->
var default_rating = 0;
<!-- END switch_user_hasnt_voted -->

function build_vote()
{
document.write('Rate it: ');
for (n = 1; n<= 5; n++)
{
if (n <= default_rating)
{
default_pic = pic_selected;
} else {
default_pic = pic_unselected;
}

document.write('<a href="#" onclick="vote(' + n + '); return false;" onmouseover="rateover(' + n + ');" onmouseout="rateover(default_rating);"><img src="' + dir + default_pic + '" name="star_' + n + '" style="border:0px;" /></a>&nbsp;');
}
document.write('');
}

function rateover(star_num)
{

for (n = 1; n <= 5; n++)
{
if (n <= star_num)
{
da_code = 'document.star_' + n + '.src="' + dir + pic_selected + '"';
} else {
da_code = 'document.star_' + n + '.src="' + dir + pic_unselected + '"';
}

eval(da_code);
}
}

function vote(rating)
{
var new_window = window.open("/ratecomic.php?cid={COMIC_ID}&rating=" + rating, "sj_ratecomic", "height=300, width=400");
new_window.focus();
}
/* ]]> */
</script>


To change the code, I've only needed to change three things, these (I have multiple versions of these three things running on the same page, so I add numbers to the functions and star names - and usually to the variables, but I renamed them in this one, so I don't need to):
var txt_unselected = ">"; 
var txt_selected = "<"; 
var txt_over = ".";

var txt_novote = new Array(); 
txt_novote[0] = "Pl"; 
txt_novote[1] = "e"; 
txt_novote[2] = "a"; 
txt_novote[3] = "s"; 
txt_novote[4] = "e?"; 

var txt_vote = new Array(); 
txt_vote[0] = "Th"; 
txt_vote[1] = "a"; 
txt_vote[2] = "n"; 
txt_vote[3] = "k"; 
txt_vote[4] = "s!";

function build_vote3()
{
document.write('Rate it: ');
for (n = 1; n<= 5; n++)
{
var nx = n - 1;
if (default_rating==0)
{
default_txt = txt_novote[nx];
} else
{
default_txt = txt_vote[nx];
}

document.write('<a href="#" Id="star3_' + n + '"  onclick="vote(' + n + '); return false;" onmouseover="rateover3(' + n + ');" onmouseout="rateover3(default_rating);">' + default_txt + ' </a>');
}
document.write('');
}

function rateover3(star_num)
{

for (n = 1; n <= 5; n++)
{
var nx = n-1;
if (n < star_num)
{
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_selected[nx]+'"';
} else if (n == star_num)
{
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_over[nx]+'"';
} else { 
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_unselected[nx]+'"';
}

eval(da_code);
}
}

Was This Post Helpful? 0
  • +
  • -

#8 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: find a link within the document with JavaScript

Posted 07 January 2010 - 08:15 AM

Try changing:

document.getElementById("star3_"+n).innerHTML = txt_selected[nx];



to just:

document.getElementById("star3_"+n).innerHTML = txt_selected;



It does not appear to be set up as an array so you don't need the brackets. Try doing that for all three (txt_selected, txt_unselected, txt_over).
Was This Post Helpful? 0
  • +
  • -

#9 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: find a link within the document with JavaScript

Posted 07 January 2010 - 09:53 AM

Oh! I forgot that wasn't one of the arrays. ^_^" Stupid me. I've been using too many arrays lately.
Was This Post Helpful? 0
  • +
  • -

#10 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: find a link within the document with JavaScript

Posted 07 January 2010 - 09:56 AM

It happens. Get so accustomed to doing things a certain way, then you change it up and keep doing the same thing. So did that make it work for you?
Was This Post Helpful? 0
  • +
  • -

#11 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: find a link within the document with JavaScript

Posted 07 January 2010 - 01:48 PM

It made it quit showing "undefined".

Then I thought maybe the arrows was messing it up somehow, so I changed this:
var txt_unselected = "<"; 
var txt_selected = ">"; 

That fixed everything but the onmouseout part...

This is what I have at the moment:

I added the variable default_ratingb because I want to distinguish default_rating from n, so I thought I could make it negative (because n is never negative)
var default_ratingb = 0 - default_rating;
document.write('<a href="#" Id="star3_' + n + '"  onclick="vote(' + n + '); return false;" onmouseover="rateover3(' + n + ');" onmouseout="rateover3(default_ratingb);">' + default_txt + ' </a>');


it seems like all the if's in this function work except for the one to check for 0 and maybe below 0...
function rateover3(star_num)
{

for (n = 1; n <= 5; n++)
{
var nx = n-1;
if (star_num == 0)
{
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_novote[nx] +'"';
} else if (n < star_num)
{
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_selected +'"';
} else if (n > star_num && star_num > 0)
{ 
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_unselected +'"';
} else if (n == star_num)
{
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_over +'"';
} else if (star_num < 0)
{ 
da_code = 'document.getElementById("star3_'+n+'").innerHTML = "' + txt_vote[nx] +'"';
}

eval(da_code);
}
}




Edit:
Hmm... when I take default_ratingb out of the quotes in the onmouseout part,
onmouseout="rateover3('+default_ratingb+');">
then it seems to work okay. But... why does it work fine within the quotes when you use images...?

This post has been edited by asj127: 07 January 2010 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#12 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: find a link within the document with JavaScript

Posted 07 January 2010 - 03:09 PM

The first way you posted it you didn't include the single quotes, that may be why it wasn't working, not sure...but if works now I wouldn't touch anything lol
Was This Post Helpful? 0
  • +
  • -

#13 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: find a link within the document with JavaScript

Posted 07 January 2010 - 03:55 PM

why do I need the ' things for the text, but not for images? Because the original code (that uses images) didn't have those and it works...
Was This Post Helpful? 0
  • +
  • -

#14 Moshambi  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 280
  • Joined: 20-November 07

Re: find a link within the document with JavaScript

Posted 07 January 2010 - 04:36 PM

It could be a number of things, but I'm guessing it has some way to do with what data type was being passed around. I would have to see the original code, I looked at your first post and it does have ' in it.
Was This Post Helpful? 0
  • +
  • -

#15 asj127  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 157
  • Joined: 29-April 09

Re: find a link within the document with JavaScript

Posted 07 January 2010 - 08:04 PM

I posted the original code - the first big block of code in this thread.
It has this (there's no ' 's around default_rating for the onmouseout part, but I need it for my modification of the code):
document.write('<a href="#" onclick="vote(' + n + '); return false;" onmouseover="rateover(' + n + ');" onmouseout="rateover(default_rating);"><img src="' + dir + default_pic + '" name="star_' + n + '" style="border:0px;" /></a> ');

This post has been edited by asj127: 07 January 2010 - 08:06 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2