Saint Robson
Saint Robson

Reputation: 5525

How To Put HTML Input Value As Variable In jQuery Load?

I have this jQuery code from my previous project and I want to modify it :

<script>
...
$('a.manage-content-link').click(function (e) {
    var self = $(this),
        file = self.siblings('input[type="hidden.block-hidden-input"]').val();
    self.next(".manage-content-wrap").find(".manage-content").load("file-" + file + ".php");
    e.preventDefault();
});
...
</script>

and this HTML code relates to it :

<li>
    <input type="hidden" name="block-type" value="0482" class="block-hidden-input" />
    <input type="hidden" name="sid" value="80132930913019309483" class="block-hidden-input" />
    <a href="#" id="manage-1" class="manage-content-link">
        <img src="images/web-block/web-block1.jpg"/>
        <span class="orange-notice">Click to Edit Content</span>    
    </a>
</li>

<li>
    <input type="hidden" name="block-type" value="1932" class="block-hidden-input" />
    <input type="hidden" name="sid" value="98u40190931209402191" class="block-hidden-input" />
    <a href="#" id="manage-2" class="manage-content-link">
        <img src="images/web-block/web-block2.jpg"/>
        <span class="orange-notice">Click to Edit Content</span> 
    </a>
</li>

and I want to modify that jQuery LOAD part into something like this :

.load("web-block/forms/file-XXX.php?key=YYYYYYYYYYYYYYYY");

where :

XXX = last 3 number from input type="hidden" name="block-type"
example :   0482 >> XXX = 482
            1932 >> XXX = 932

and :

YYYYYYYYYYYYYYYY = value from input type="hidden" name="sid"

please note, for this project I've modified the HTML part so it has 2 hidden inputs. so I believe jQuery selection needs to be modified too... but how?

Upvotes: 0

Views: 293

Answers (2)

Om3ga
Om3ga

Reputation: 32893

Use e.preventDefault() at the beginning in un-anonymous function

<li>
<input type="hidden" name="name-block-type" value="0482" class="block-hidden-input" />
<input type="hidden" name="name-sid" value="80132930913019309483" class="block-hidden-input" />
<a href="#" id="manage-1" class="manage-content-link">
    <span class="orange-notice">Click to Edit Content</span>    
</a>
</li>

<li>
<input type="hidden" name="name-block-type" value="1932" class="block-hidden-input" />
<input type="hidden" name="name-sid" value="98u40190931209402191" class="block-hidden-input" />
<a href="#" id="manage-2" class="manage-content-link">
    <span class="orange-notice">Click to Edit Content</span> 
</a>
</li>​

JS

$('a.manage-content-link').click(function (e) {
e.preventDefault();
var self = $(this),
    name = self.parent().find('input[name^=name]').val(),
    lastThree = file.substr(file.length - 3);
self.next(".manage-content-wrap").find(".manage-content").load("web-block/forms/file-" + lastThree + ".php");
});​

Upvotes: 1

Mihai Matei
Mihai Matei

Reputation: 24276

$('a.manage-content-link').click(function (e) {
    var self = $(this),
        file = self.siblings('input[name="block-type"]').val();
    file = file.substring(file.length - 3);
    self.next(".manage-content-wrap").find(".manage-content").load("web-block/forms/file-" + file + ".php?key=" + self.siblings('input[name="sid"]').val());
    e.preventDefault();
});

Upvotes: 1

Related Questions