1

HTML source:

<span class="specLink">
    <specialty><a title="Plastic Surgery" href="link2.aspx">Plastic Surgery</a></specialty>
</span>
<br />
<span class="specLink">
    <specialty2><a title="Hand Surgery" href="link3.aspx">Hand Surgery</a></specialty2>
</span>

How can I create a JQuery script which runs during page load to displays the same list taking from the HTML Source listed above?

E.g.:

<div class="justPad">
    <a title="Plastic Surgery" href="link2.aspx" class="defaultLinks">Plastic Surgery</a>
</div>
<div class="justPad">
    <a title="Hand Surgery" href="link3.aspx" class="defaultLinks">Hand Surgery</a>
</div>

How I would like it to be:

var k = "";
$(".specLink").each(function() {
    var aLink = $(".specLink").replace(<%-- Remove the <specialty#></specialty#> tags and only keep the anchor link --%>);

    k += '<div class="justPad">'; //.. as many entries that shows up
    k += aLink; //.. as many entries that shows up
    k += '</div>'; //.. as many entries that shows up
});

//Once I have added 
$(".addSpecialties").html(k);

Blank HTML:

<div class="serviceHolder brClear addSpecialties">
    //add inside here from the JQuery above
</div>
5
  • Can you just show your HTML input and desired HTML output? Commented Dec 3, 2014 at 17:10
  • Sounds like you need AngularJS. Here is a free course campus.codeschool.com/courses/shaping-up-with-angular-js/intro Commented Dec 3, 2014 at 17:28
  • @JorgeZuverza "Sounds like you need AngularJS" - I beg your pardon?! Commented Dec 3, 2014 at 17:33
  • 1
    Why would I use AngularJS when I am already using JQuery? Commented Dec 3, 2014 at 17:35
  • 1
    AngularJS and jQuery solve very different problems. The real question is "Why would that problem indicate that I need AngularJS?", and I'm sure JorgeZuverza would have a pretty hard time explaining that. Commented Dec 3, 2014 at 17:39

1 Answer 1

2

Something like:

var specialties = $(".specLink a").map(function() {
    return $('<div class="justPad">').append( $(this).clone() )[0];
}).toArray();

$(".addSpecialties").empty().append(specialties);
Sign up to request clarification or add additional context in comments.

6 Comments

Do I have to add the closing </div>?
@SearchForKnowledge nope, <div... here is not an opening tag but is just a string that orders jQuery to create new div with class 'justPad'.
@SearchForKnowledge No, but you can, if it helps you relax. ;-) jQuery does the right thing without it.
Yes it sure will help me relate... :D... Do I just add that before the [0]?
No. You would use $('<div class="justPad"></div>'). I think you need to take a break and learn jQuery basics. You're moving to fast.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.