Search code examples
jqueryhrefattr

jquery adding variables to href attribute


I'm sure this is probably something simple I'm overlooking but I can't figure it out for the life of me. Any help is greatly appreciated.

HTML

<h1 class="entry-title">Installation Maintenance and Detailed Inspection</h1>
<div class="ai1ec-time">
    <div class="ai1ec-label">When:</div>
    <div class="ai1ec-field-value">January 13, 2014</div>
</div>
<div class="ai1ec-location">
    <div class="ai1ec-label">Where:</div>
    <div class="ai1ec-field-value">MOXI Perth</div>
</div>
<div class="ai1ec-cost">
    <div class="ai1ec-label">Cost:</div>
    <div class="ai1ec-field-value">AU$3200</div>    
</div>
<a id="booklink" title="Training Enrolement Form" href="http://www.moxi.com.au/training-enrolement-form/">Book Now!</a>

jQuery

<script>
jQuery(document).ready(function(){
    var bookcourse = jQuery(".entry-title").html;
    var booktime =  jQuery(".ai1ec-time .ai1ec-field-value").html;
    var booklocation =  jQuery(".ai1ec-location .ai1ec-field-value").html;
    var bookcost =  jQuery(".ai1ec-cost .ai1ec-field-value").html;
    jQuery("#booklink").attr('href',"http://www.moxi.com.au/training-enrolement-form?title="+bookcourse+"&cost="+bookcost+"&date="+booktime+"&location="+booklocation);
});
</script>

Goal is to have the href attribute come out as:

http://www.moxi.com.au/training-enrolement-form?title=Installation Maintenance and Detailed Inspection&cost=AU$3200&date=January 13, 2014&location=MOXI Perth

Solution

  • html() is a method not a property, so you need to use .html() not .html

    var bookcourse = jQuery(".entry-title").html();
    var booktime =  jQuery(".ai1ec-time .ai1ec-field-value").html();
    var booklocation =  jQuery(".ai1ec-location .ai1ec-field-value").html();
    var bookcost =  jQuery(".ai1ec-cost .ai1ec-field-value").html();
    

    Demo: Fiddle