Jump to content

Target dynamic content


Go to solution Solved by dpiearcy,

Recommended Posts

You guys always help.  I'm NOT a javascript programmer but am learning slowly.  Here's the situation:

 

I'm loading dynamic content I originally created for PHP and MySql for this sites main page.  No problems there.  I savvy PHP.  But now I'm building a phone app using juerymobile (mostly... lot's of custom javascript in there as well).  So I'm calling for the dynamic content to be loaded via AJAX and JSONP.  No problems there either.  (basically getting that info from a PHP file of course that gets the info from the database).  

 

Now in PHP once I have that ID for the content I want I can manipulate things to my hearts desire.  Since I'm not a javascript programmer it's hard for me to wrap my head around the way to accomplish what I can with PHP.  

 

I'm truncating data.  I want to slide to a new view when an arrow is clicked.  So I don't have to call for the data again, I'm hiding a div that slides into view when clicked.  But I need the content of that particular row (in MySql) and only that row to appear in that div.  I have that data in a var but since it's in a loop....  

 

I can pass the ID on to javascript if that helps.  

 

Some good reading would be nice to increase my learning.  Stupid class I took online didn't get into these kinds of details.  

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/278652-target-dynamic-content/
Share on other sites

  • Solution

I THINK I've found my answer.  I stumbled across this bit of code and I believe it does exactly what I want it to do.  Now just to change it to fit my application.  

(function(){
        
    // querySelector, jQuery style
    var $ = function (selector) {
        return document.querySelector(selector);
    };
    
    // Create function outside loop
    function dynamicEvent() {
        this.innerHTML = 'Dynamic event success.';
        this.className += ' dynamic-success';
    }
    
    // Iterate over #links <li>
    // Use querySelector to target #links and then get tag names <li>
    var links = $('#links').getElementsByTagName('li');
    
    // For each <li> inside #links
    for (var i = 0; i < links.length; i++) {
        var link = links[i];
        
        // <li> onclick, runAlert function
        link.onclick = dynamicEvent;
    }
    
    // Onsubmit
    $('.generate').onsubmit = function() {
    
        // Grab the input value
        var dynamicValue = $('.generate-input').value;
        
        // If empty value
        if(!dynamicValue) {
        
            alert('Please enter something.');
            
        } else {
        
            // Change the submit value
            $('.generate-submit').value = 'Click your item below!';
            
            // Create the links with the input value as innerHTML
            var li = document.createElement('li');
            li.className = 'dynamic-link';
            li.innerHTML = dynamicValue;
            
            // Append it and attach the event (via onclick)
            $('#links').appendChild(li);
            li.onclick = dynamicEvent;
        }
        
        // Prevent the form submitting
        return false;
    }
})();

I'm using a bunch of var's with div's etc. in them but the principle is the same.  Thanks anyway.  

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.