Javascript Help, interaction with HTML

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Javascript Help, interaction with HTML

Postby sauced » Fri Apr 13, 2012 3:32 am UTC

Hey Guys,
I'm pretty fluent with HTML, PHP, and Java and at the moment am trying to get a dynamic site working. My problem is javascript, I have looked in a lot of places for help but never finding anything. What I have is an <input type="hidden" value=""> inside a form that sends to a php script that preps the information elsewhere on the site, also on the page I have <li> tags that are generated by php and include a name between the open and close tag with an id attribute. What I need to happen in javascript is to obtain that id when a user clicks on the <li> and use that as a variable to change the value in the hidden input and do some other things like change a nearby picture and make some ajax calls, I have so far had no success in getting the variable to change OR get the value to change. If anyone has any advice that would be awesome. I thank you for whatever help is possible!
sauced
 
Posts: 3
Joined: Fri Apr 13, 2012 3:18 am UTC

Re: Javascript Help, interaction with HTML

Postby Maelstrom. » Fri Apr 13, 2012 7:22 am UTC

Try something along the lines of this: http://jsfiddle.net/sfdHM/

The description of your problem was a little ambiguous, so I probably have not done exactly what you want. What I have done though is change various things when ever the <li> elements are clicked, which seemed to be the core of your problem. If you are using a JavaScript library like jQuery, this code could be made simpler, but I did not want to assume any libraries. This code will not work correctly if you have multiple <li> elements on a page. Fixing that problem is left as an excersize for the reader :D
Maelstrom.
 
Posts: 60
Joined: Tue Oct 21, 2008 12:18 pm UTC

Re: Javascript Help, interaction with HTML

Postby Xanthir » Fri Apr 13, 2012 4:52 pm UTC

Maelstrom's code is right. Feel free to learn from that.

In the future, including samples of your code that isn't working is a lot more helpful.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))
User avatar
Xanthir
My HERO!!!
 
Posts: 4023
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex

Re: Javascript Help, interaction with HTML

Postby Steax » Fri Apr 13, 2012 4:56 pm UTC

If you're intending to further work with it (and I assume yes, since you mentioned AJAX) I too suggest grabbing a library. jQuery is the current industry 'standard', I believe. It also tends to be easier for new users to learn.
In Minecraft, I use the username Rirez.
User avatar
Steax
SecondTalon's Goon Squad
 
Posts: 2772
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Javascript Help, interaction with HTML

Postby sebwiers » Wed Apr 25, 2012 1:56 am UTC

Seconded. Here's an example that's veryy easy to implement in PHP (assuming you are using a loop to creat the <li> elements) that uses jquery:
Code: Select all
<ul>
    <li><a onclick="$('#hidIn').val('One')">One</a></li>
    <li><a onclick="$('#hidIn').val('Two')">Two</a></li>
</ul>
<input type="hidden" id="hidIn" value="">
sebwiers
 
Posts: 18
Joined: Sat Apr 21, 2012 3:12 pm UTC

Re: Javascript Help, interaction with HTML

Postby Steax » Wed Apr 25, 2012 3:42 am UTC

The "correct" way of doing that would be by storing the value you want to attached to the hidden input in a data attribute, and then using jQuery's .on('click') to attach the handler.

Code: Select all
$(document).ready(function(){

    $(".selections li a").on('click', function(){
        $("#hidIn").val($(this).attr('data-value'));
        return false;
    });

});


Code: Select all
<ul class="selections">
    <li><a href="#" data-value="One">One</a></li>
    <li><a href="#" data-value="Two">Two</a></li>
</ul>
<input type="hidden" id="hidIn" value="">
In Minecraft, I use the username Rirez.
User avatar
Steax
SecondTalon's Goon Squad
 
Posts: 2772
Joined: Sat Jan 12, 2008 12:18 pm UTC


Return to Coding

Who is online

Users browsing this forum: No registered users and 10 guests