add html attribute value via css/javascript

nullbytes

New Member
I want to add an attribute to a html form.
The html code is

<input type="text" name="nm-form-meta[<?php echo $tag?>]" id="<?php echo $tag.'-'.$widget_id?>" class="nm_mc_input" />n

dynamic content is $tag , it gives two values in two 'text box' output.

<input type="text" name="nm-form-meta" will add value =" Enter your email"
<input type="text" name="nm-form-meta[FNAME]" will add value =" Enter your email"

i tried with php like this

<input type="text" name="nm-form-meta[<?php echo $tag?>]" value=" <?php if ($tag="EMAIL") { echo "Enter your email";} else { echo "enter your Name";} ?>" id="<?php echo $tag.'-'.$widget_id?>" class="nm_mc_input" />n

but it did not work.

i putted this jquery code in head tag

<script type="text/javascript">
$('#nmedia_mail_chimp-2 input[name="nm-form-meta[EMAIL]"]').attr('value', 'email');
$('#nmedia_mail_chimp-2 input[name="nm-form-meta[FNAME]"]').attr('value', 'First name');
</script>

does not work either.

how can i implement this. Please help me
 
Last edited:

daviedR

New Member
Code:
<script type="text/javascript">
$('#nmedia_mail_chimp-2 input[name="nm-form-meta[EMAIL]"]').attr('value', 'email');
$('#nmedia_mail_chimp-2 input[name="nm-form-meta[FNAME]"]').attr('value', 'First name');
</script>

I think your script for jquery is already good. But if you place it on the <head> you should wrap your script with something like

Code:
$(document).ready(function() {
  // .... your original code here ....
});

Browsers read the script of your markup from top to bottom. So if you put your javascript in the <head>, the browser still doesn't know the element you are targeting. So, adding an event handler on document ready is the best practice, to wait the markup completely loaded then javascript know the elements you are targetting.

Let's see if you still have problem with it :D
 

Phreaddee

Super Moderator
Staff member
which is exactly why I use the rule:-
css at the top of the document
js at the bottom of the document.
 

nullbytes

New Member
it is workin now. i forgot about $(document).ready . i putted the code in footer and the forms have value attribute.
 

nullbytes

New Member
after uploading the file to my web server it stopped working. For your acknowledgement, that js code worked OFFLINE only when i connected to internet. It loads few script. I use xampp server in my pc.
Why it is not working now? i assume there can be a js library conflict. Please check it at pcgeek.info/blog
 

chrishirst

Well-Known Member
Staff member
Well you have a LOT of errors in the js code, I'd suggest using the Firefox error console and working through them.
 

daviedR

New Member
Do you have any error message of the conflicted library?

Sometimes, it happens when $(document) is not recognized by the browser. It caused the conflict because the $ is already used by another library besides jQuery. If this is really the cause of your problem try this:

Code:
jQuery(document).ready(function($) {
  ... your code here ...
});

for later use inside that code, you could represent jQuery with $..

By the way, it's the best practice to put any custom javascript code (non-referenced) before the close tag of your body element (before </body>). It's the best because your inside body elements have been loaded completely, so it's time to play the javascript.

Tell us if you still have problem :)
 
Top