0

I have a div where I would like to get the value and use it inside an onclick event.

Here's the div.

<div id="cars">100</div>

Then I need to enter the id cars text or html into the message below.

So I need to insert the 100 where is said INSERT HERE in the code below.

<img onclick="window.plugins.socialsharing.shareViaTwitter('Message: Hello  INSERT HERE')" src="images/share_text.png" style="height:2em" />

How can I do this?

4 Answers 4

4

In plain javascript:

document.getElementById('cars').innerHTML

or with jQuery:

$('#cars').text()
Sign up to request clarification or add additional context in comments.

1 Comment

Nice that you demonstrated both ways. One is just 40 bytes and the other evil way over 80000 bytes
3

This will be very simple using jquery

$("#cars").text();

Will give you the text

Comments

1

If you want the jQuery solution use .html() to get the text between the start/close div tags.

<img onclick="window.plugins.socialsharing.shareViaTwitter('Message: Hello' + $('#cars').html())" src="images/share_text.png" style="height:2em" />

Comments

1

Its not suggested to include jquery/ javascript code in html code. I suggest you can do this on document.ready event as below, you need to give an "id" to your image tag also.

HTML code (add id to image tag)

<div id="cars">100</div>
<img onclick="window.plugins.socialsharing.shareViaTwitter('Message: Hello  INSERT HERE')" src="images/share_text.png" style="height:2em" id="image_id" />

Jquery Code:

 $( document ).ready(function() {
    var car_val = $("#cars").html(); 
    var onclick_Val = $("#image_id").attr("onclick");
    var onclick_new_val = onclick_Val.replace('INSERT HERE', car_val);
    $("#image_id").attr( 'onclick', onclick_new_val ); 
 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.