Call a simple javascript function

96 views Asked by At

I have this code where I insert a read more link after a label with a specific ID:

$( "<a class='readMore' href='/moreByggherre' id='moreByggherre'>Läs mer</a>" ).insertAfter( $("label[for='Byggherre']") );

The above code works, but since I will be doing this a lot I would like to have a function which just takes the ID and outputs the html code with inserAfter.

I tried this, but it doesn't seem to work:

createReadMoreBtn('Byggherre');
function createReadMoreBtn($id){
    $insertTxt = "<a class='readMore' href='/more".$id."' id='more".$id."'>Läs mer</a>";
    $( $insertTxt ).insertAfter( $("label[for='".$id."']") );
}
2

There are 2 answers

1
kemal akoğlu On

Could you try this;

 createReadMoreBtn('Byggherre');

    function createReadMoreBtn($id)
    {
        $insertTxt = "<a class='readMore' href='/more"+$id+"' id='more"+$id+"'>Läs mer</a>";
        $( $insertTxt ).insertAfter( $("label[for='"+$id+"']") );
    }
0
Pranav C Balan On

It's not PHP to use . for concatenation instead, you need to use + for concatenation in JavaScript.

createReadMoreBtn('Byggherre');

function createReadMoreBtn($id)
{
    $insertTxt = "<a class='readMore' href='/more" + $id + "' id='more" + $id + "'>Läs mer</a>";
    $( $insertTxt ).insertAfter( $("label[for='" + $id + "']") );
}