Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2011, 12:56 AM   PM User | #1
broadbeach
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
broadbeach is an unknown quantity at this point
find a value inside of class and display a link

Hi all we are in desperate need of a solution, we have tried and search different methods but none of us can figure it out.

Basically we need to find a word which is inside a class and display a link.

So something like this

find a word equal to "1 line custom"

then

display this <a href="/1-line-custom.htm">click here</a>

I'm not sure if this is really simple or not.

Any help would be much appreciated.

Thanks

Will
broadbeach is offline   Reply With Quote
Old 08-19-2011, 01:14 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
What does "inside a class" mean? What kind of class? You mean a class name, as in
Code:
<span class="bananas">...</span>
An example would help tremendously, you know.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
broadbeach (08-19-2011)
Old 08-19-2011, 01:33 AM   PM User | #3
broadbeach
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
broadbeach is an unknown quantity at this point
Ok so we are getting close, basically what this is doing now is finding the words inside a class then showing us them in an alert.

Code:
<table>
  <tbody>
    <tr>
      <td><div class="productitemcell">Champagne Weadding</div></td>
      <td><div class="productitemcell">1 line custom</div></td>
    </tr>
  </tbody>
</table>
Code:
function displaymessageNow()
{
var divArray = document.getElementsByClassName("productitemcell");

for (var i = 0; i<divArray.length; i++)
{
if (divArray[i].class="productitemcell")
alert(divArray[i].innerHTML);
}
}
displaymessageNow();
But this is obviously not the final result we are after.

We need to be able to find the words "1 line custom" in the background (so the user dosnt see this happening) then once these words are found display a link.

Let me know if this is making any sense to you.

Thanks
broadbeach is offline   Reply With Quote
Old 08-19-2011, 01:41 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay, pretty trivial. There are better ways to do this, and it may not work for all kinds of content, but given the example you showed, just do:
Code:
function addLinks( )
{
    var divArray = document.getElementsByClassName("productitemcell");
    for (var i = 0; i<divArray.length; i++)
    {
        var div = divArray[i[;
        div.innerHTML = '<a href="/' + escape(div.innerHTML) + '">' + div.innerHTML + '</a>';
    }
}
window.onload = addLinks;
With the example you gave, that would alter it to
Code:
<table>
  <tbody>
    <tr>
      <td><div class="productitemcell"><a href="/Champagne%20Weadding">Champagne Weadding</a></div></td>
      <td><div class="productitemcell"><a href="/1%20line%20custom">1 line custom</a></div></td>
    </tr>
  </tbody>
</table>
The call to escape() is there because spaces in URLs can cause hiccups, so you encode the URL (and the browser will automatically decode it...converting %20 to space, etc.).

Is that what you meant?
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
broadbeach (08-19-2011)
Old 08-19-2011, 02:04 AM   PM User | #5
broadbeach
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
broadbeach is an unknown quantity at this point
Ok ok, I'm liking this that is very very close to what we are after, however there is still a small issue.

It would be preferable if the first two div classes "Champagne Weadding" and "Party Like 1999" didnt generate as links.

Code:
<table>
  <tbody>
    <tr>
      <td><div class="productitemcell">Champagne Weadding</div>
        <div class="productitemcell">Party Like 1999</div></td>
      <td><div class="productitemcell">1 line custom</div>
        <div class="productitemcell">2 line custom</div></td>
    </tr>
  </tbody>
</table>

Now keeping in mind that we have no control over removing the <div class="productitemcell"></div> wrapped around them as this is a function of the CMS we are using, However we could wrap it all in a div id or something if that would help like this:

Code:
<table>
  <tbody>
    <tr>
      <td><div id="removeLink">
          <div class="productitemcell">Champagne Weadding</div>
          <div class="productitemcell">Party Like 1999</div>
        </div></td>
      <td><div class="productitemcell">1 line custom</div>
        <div class="productitemcell">2 line custom</div></td>
    </tr>
  </tbody>
</table>
Thanks
broadbeach is offline   Reply With Quote
Old 08-19-2011, 02:13 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Would be better if you could wrap around the ones you *want*:
Code:
<table>
  <tbody>
    <tr>
      <td>
          <div class="productitemcell">Champagne Weadding</div>
          <div class="productitemcell">Party Like 1999</div>
      </td>
      <td>
        <div id="changeLinks">
            <div class="productitemcell">1 line custom</div>
            <div class="productitemcell">2 line custom</div>
        </div>
      </td>
    </tr>
  </tbody>
</table>
Is that possible?

If not, yes we can do it, just more work.
Old Pedant is offline   Reply With Quote
Old 08-19-2011, 02:17 AM   PM User | #7
broadbeach
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
broadbeach is an unknown quantity at this point
Yes we can do what you suggested above.
broadbeach is offline   Reply With Quote
Old 08-19-2011, 02:22 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay...
Code:
function addLinks( )
{
    var changeOnly = document.getElementsByClassName("changeLinks");
    for ( var c = 0; c < changeOnly.length; ++i )
    {
        var divArray = changeOnly[c].getElementsByClassName("productitemcell");
        for (var i = 0; i<divArray.length; i++)
        {
            var div = divArray[i[;
            div.innerHTML = '<a href="/' + escape(div.innerHTML) + '">' + div.innerHTML + '</a>';
        }
    }
}
window.onload = addLinks;
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
broadbeach (08-19-2011)
Old 08-19-2011, 02:23 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
To do it the other way isn't *that* much harder, though, if it's significantly easier for you do drop in <div class=removeLink">
Old Pedant is offline   Reply With Quote
Old 08-19-2011, 02:44 AM   PM User | #10
broadbeach
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
broadbeach is an unknown quantity at this point
fantastic, we just had to change the following:
Code:
function addLinks()
{
var changeOnly = document.getElementsByClassName("changeLinks");
for ( var c = 0; c<changeOnly.length; ++c )
{
var divArray = changeOnly[c].getElementsByClassName("productitemcell");
for (var i = 0; i<divArray.length; i++)
{
var div = divArray[i];
div.innerHTML = '<a href="/' + escape(div.innerHTML) + '">' + div.innerHTML + '</a>';
}
}
}
window.onload = addLinks;
broadbeach is offline   Reply With Quote
Old 08-19-2011, 03:44 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oops...too much copy/paste, not enough edit. Sorry.
Old Pedant is offline   Reply With Quote
Old 08-22-2011, 08:28 AM   PM User | #12
broadbeach
New Coder

 
Join Date: Aug 2011
Posts: 15
Thanks: 8
Thanked 0 Times in 0 Posts
broadbeach is an unknown quantity at this point
Hey Old Pedant

I have another problem with the same code as in previous posts if you wish to accept.

We are trying to add thick box to the links which are created. At first this seemed rather simple, however it dosnt want to work.

Here is the code which triggers thick box, now this works all fine.

Code:
<a href="/customise-forms/customisation-1?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=200&width=300&modal=true" title="Form 1" class="thickbox">customisation-1</a>

Now here is the thick box code implemented into the JS you wrote for us previously (thick box stuff in red):

Code:
function addLinks()
{
	var changeOnly = document.getElementsByClassName("changeLinks");
	for ( var c = 0; c<changeOnly.length; ++c )
	{
		var divArray = changeOnly[c].getElementsByClassName("productitemcell");
		for (var i = 0; i<divArray.length; i++)
		{
			var div = divArray[i];
			div.innerHTML = '<a href="/customise-forms/' + escape(div.innerHTML) + '?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=200&width=300&modal=true" class="thickbox" title="Form 1">' + div.innerHTML + '</a>';
		}
	}
}
window.onload = addLinks;
The URL it spits out seems to be fine and in theory it should work but as it is it just sends you to the actual page instead of opening up in thick box. Can you see anything in the code that would stop the thick box from working.

Thanks Mate
broadbeach is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:48 AM.


Advertisement
Log in to turn off these ads.